1--TEST--
2mysqli_fetch_array()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('skipifconnectfailure.inc');
8?>
9--FILE--
10<?php
11	require("table.inc");
12
13	// do as much as we can do in 5 seconds
14	$start = microtime(true);
15	for ($id = 100, $start = microtime(true); (microtime(true) - $start) < 5; $id++) {
16		if (!mysqli_query($link, $sql = sprintf("INSERT INTO test(id, label) VALUES (%d, '%s')",
17			$id, mysqli_real_escape_string($link, chr(65 + ($id % 26)))))) {
18				printf("[001] %s failed: [%d] %s\n", $sql, mysqli_errno($link), mysqli_error($link));
19				break;
20		}
21	}
22
23	if (!$res = mysqli_query($link, 'SELECT id, label FROM test')) {
24		printf("[002] SELECT failed: [%d] %s\n", mysqli_errno($link), mysqli_errno($link));
25	}
26
27	while ($row = mysqli_fetch_array($res)) {
28		// overwrite results and check if the cache magic works
29		$row['label'] = NULL;
30	}
31	mysqli_free_result($res);
32
33	if (!$res = mysqli_query($link, 'SELECT id, label FROM test')) {
34		printf("[003] SELECT failed: [%d] %s\n", mysqli_errno($link), mysqli_errno($link));
35	}
36
37	$i = 0;
38	$results = array();
39	while ($row = mysqli_fetch_array($res, MYSQLI_BOTH)) {
40
41		// create copies and destroy later
42		$results[$i++] = &$row;
43		if ($i % 999) {
44			$results = array();
45		}
46
47		if ($row[0] < 0 || $row[0] > $id) {
48			printf("[004] Unexpected result row[0] = '%s' (range 0...%d), [%d] %s\n",
49				$row[0], $id, mysqli_errno($link), mysqli_error($link));
50			break;
51		}
52		if ($row[0] !== $row['id']) {
53			printf("[005] Unexpected result row[0] = '%s', row[id] = '%s', [%d] %s\n",
54				$row[0], $row[id], mysqli_errno($link), mysqli_error($link));
55			break;
56		}
57
58		$len = strlen($row[1]);
59		if (!is_string($row[1]) || $len == 0 || $len > 1) {
60			printf("[006] Unexpected result row[1] = '%s', [%d] %s\n",
61				$row[1], mysqli_errno($link), mysqli_error($link));
62			break;
63		}
64		if ($row[1] !== $row['label']) {
65			printf("[007] Unexpected result row[1] = '%s', row[label] = '%s', [%d] %s\n",
66				$row[1], $row['label'], mysqli_errno($link), mysqli_error($link));
67			break;
68		}
69
70	}
71	mysqli_free_result($res);
72
73	if (!$res = mysqli_query($link, 'SELECT id, label FROM test')) {
74		printf("[008] SELECT failed: [%d] %s\n", mysqli_errno($link), mysqli_errno($link));
75	}
76
77	while ($row = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
78		// overwrite results and check if the cache magic works
79		$row['label'] = NULL;
80	}
81	mysqli_free_result($res);
82
83	if (!$res = mysqli_query($link, 'SELECT count(*) AS num FROM test')) {
84		printf("[009] SELECT failed: [%d] %s\n", mysqli_errno($link), mysqli_errno($link));
85	}
86	$row = mysqli_fetch_assoc($res);
87	$num = $row['num'];
88	mysqli_free_result($res);
89
90	if (!$res = mysqli_query($link, 'SELECT id, label FROM test')) {
91		printf("[010] SELECT failed: [%d] %s\n", mysqli_errno($link), mysqli_errno($link));
92	}
93
94	$i = 0;
95	while ($row = mysqli_fetch_array($res, MYSQLI_NUM)) {
96		// overwrite results and check if the cache magic works
97		$row[0] = NULL;
98		$i++;
99	}
100	mysqli_free_result($res);
101
102	if ($i != $num)
103		printf("[011] Expecting %d results, got %d results, [%d] %s\n",
104			$num, $i, mysqli_errno($link), mysqli_error($link));
105
106	mysqli_close($link);
107
108	print "done!";
109?>
110--CLEAN--
111<?php
112	require_once("clean_table.inc");
113?>
114--EXPECTF--
115done!