1--TEST--
2mysqli_stmt_get_result() - seeking
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once 'skipifconnectfailure.inc';
8
9if (!function_exists('mysqli_stmt_get_result'))
10    die('skip mysqli_stmt_get_result not available');
11?>
12--FILE--
13<?php
14    require 'table.inc';
15
16    if (!$stmt = mysqli_stmt_init($link))
17        printf("[001] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
18
19    if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id ASC LIMIT 3"))
20        printf("[002] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
21
22    if (!mysqli_stmt_execute($stmt))
23        printf("[003] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
24
25    if (!is_object($res = mysqli_stmt_get_result($stmt)) || 'mysqli_result' != get_class($res)) {
26        printf("[004] Expecting object/mysqli_result got %s/%s, [%d] %s\n",
27            gettype($res), $res, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
28    }
29
30    if (3 !== $res->num_rows)
31        printf("[005] Expecting 3 rows, got %s/%s rows\n", gettype($res->num_rows), $res->num_rows);
32
33    if (2 !== $res->field_count)
34        printf("[006] Expecting 2 fields, got %s/%s rows\n", gettype($res->field_count), $res->field_count);
35
36    if (0 !== $res->current_field)
37        printf("[006] Expecting offset 0, got %s/%s rows\n", gettype($res->current_field), $res->current_field);
38
39    for ($i = 2; $i > 0; $i--) {
40        if (!$res->data_seek($i))
41            printf("[007] Cannot seek to position %d, [%d] %s\n",
42                $i, mysqli_stmt_errno($stmt), $stmt->error);
43        $row = $res->fetch_array(MYSQLI_BOTH);
44        if (($row[0] !== $row['id']) || ($row[0] !== $i + 1)) {
45            printf("[008] Record looks wrong, dumping data\n");
46            var_dump($row);
47        } else {
48            unset($row[0]);
49            unset($row['id']);
50        }
51        if ($row[1] !== $row['label']) {
52            printf("[009] Record looks wrong, dumping data\n");
53            var_dump($row);
54        } else {
55            unset($row[1]);
56            unset($row['label']);
57        }
58        if (!empty($row)) {
59            printf("[010] Not empty, dumping unexpected data\n");
60            var_dump($row);
61        }
62    }
63
64    try {
65        $res->data_seek(-1);
66    } catch (\ValueError $e) {
67        echo $e->getMessage() . \PHP_EOL;
68    }
69
70    if (false !== ($tmp = $res->data_seek($res->num_rows + 1)))
71        printf("[012] Expecting boolean/false got %s/%s\n", gettype($tmp), $tmp);
72
73    for ($i = 0; $i < 100; $i++) {
74        /* intentionally out of range! */
75        $pos = mt_rand(-1, 4);
76        try {
77            $tmp = @mysqli_data_seek($res, $pos);
78            if (($pos >= 0 && $pos < 3)) {
79                if (true !== $tmp)
80                    printf("[015] Expecting boolan/true got %s/%s\n", gettype($tmp), $tmp);
81                $row = $res->fetch_array(MYSQLI_NUM);
82                if ($row[0] !== $pos + 1)
83                    printf("[016] Expecting id = %d for pos %d got %s/%s\n",
84                        $pos + 1, $pos, gettype($row[0]), $row[0]);
85            } else {
86                if (false !== $tmp)
87                    printf("[014] Expecting boolan/false got %s/%s\n", gettype($tmp), $tmp);
88            }
89        } catch (\ValueError $e) { /* Suppress because RANDOM */}
90    }
91
92    mysqli_stmt_close($stmt);
93
94    if (true !== ($tmp = mysqli_data_seek($res, 0)))
95        printf("[015] Expecting boolan/true got %s/%s\n", gettype($tmp), $tmp);
96
97    if (!is_array($row = $res->fetch_array(MYSQLI_NUM)))
98        printf("[016] Expecting array got %s/%s\n", gettype($tmp), $tmp);
99
100    mysqli_free_result($res);
101
102    try {
103        mysqli_data_seek($res, 0);
104    } catch (Error $exception) {
105        echo $exception->getMessage() . "\n";
106    }
107
108    try {
109        $res->fetch_array(MYSQLI_NUM);
110    } catch (Error $exception) {
111        echo $exception->getMessage() . "\n";
112    }
113
114    mysqli_close($link);
115
116    try {
117        mysqli_data_seek($res, 0);
118    } catch (Error $exception) {
119        echo $exception->getMessage() . "\n";
120    }
121
122    try {
123        $res->fetch_array(MYSQLI_NUM);
124    } catch (Error $exception) {
125        echo $exception->getMessage() . "\n";
126    }
127
128    print "done!";
129?>
130--CLEAN--
131<?php
132    require_once 'clean_table.inc';
133?>
134--EXPECT--
135mysqli_result::data_seek(): Argument #1 ($offset) must be greater than or equal to 0
136mysqli_result object is already closed
137mysqli_result object is already closed
138mysqli_result object is already closed
139mysqli_result object is already closed
140done!
141