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