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