1--TEST--
2mysqli_stmt_get_result() - seeking
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
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	if (false !== ($tmp = $res->data_seek(-1)))
65		printf("[011] Expecting boolean/false got %s/%s\n", gettype($tmp), $tmp);
66
67	if (false !== ($tmp = $res->data_seek($res->num_rows + 1)))
68		printf("[012] Expecting boolean/false got %s/%s\n", gettype($tmp), $tmp);
69
70	if (NULL !== ($tmp = $res->data_seek(PHP_INT_MAX + 1)))
71		printf("[013] Expecting NULL 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		$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	}
89
90	mysqli_stmt_close($stmt);
91
92	if (true !== ($tmp = mysqli_data_seek($res, 0)))
93		printf("[015] Expecting boolan/true got %s/%s\n", gettype($tmp), $tmp);
94
95	if (!is_array($row = $res->fetch_array(MYSQLI_NUM)))
96		printf("[016] Expecting array got %s/%s\n", gettype($tmp), $tmp);
97
98	mysqli_free_result($res);
99
100	if (NULL !== ($tmp = mysqli_data_seek($res, 0)))
101		printf("[017] Expecting NULL got %s/%s\n", gettype($tmp), $tmp);
102
103	if (NULL !== ($row = $res->fetch_array(MYSQLI_NUM)))
104		printf("[018] Expecting NULL got %s/%s\n", gettype($tmp), $tmp);
105
106	mysqli_close($link);
107
108	if (NULL !== ($tmp = mysqli_data_seek($res, 0)))
109		printf("[019] Expecting NULL got %s/%s\n", gettype($tmp), $tmp);
110
111	if (NULL !== ($row = $res->fetch_array(MYSQLI_NUM)))
112		printf("[020] Expecting NULL got %s/%s\n", gettype($tmp), $tmp);
113
114	print "done!";
115?>
116--CLEAN--
117<?php
118	require_once("clean_table.inc");
119?>
120--EXPECTF--
121Warning: mysqli_result::data_seek() expects parameter 1 to be integer, float given in %s on line %d
122
123Warning: mysqli_data_seek(): Couldn't fetch mysqli_result in %s on line %d
124
125Warning: mysqli_result::fetch_array(): Couldn't fetch mysqli_result in %s on line %d
126
127Warning: mysqli_data_seek(): Couldn't fetch mysqli_result in %s on line %d
128
129Warning: mysqli_result::fetch_array(): Couldn't fetch mysqli_result in %s on line %d
130done!
131