1--TEST--
2mysqli_store_result()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('skipifconnectfailure.inc');
8if (!$IS_MYSQLND) {
9    die("SKIP mysqlnd only test");
10}
11?>
12--INI--
13mysqlnd.debug="d:t:O,{TMP}/mysqlnd.trace"
14mysqlnd.net_read_buffer_size=1
15mysqlnd.mempool_default_size=1
16mysqlnd.fetch_data_copy=0
17--FILE--
18<?php
19    require_once("connect.inc");
20
21    $tmp    = NULL;
22    $link   = NULL;
23
24    require('table.inc');
25
26    if (!$res = mysqli_real_query($link, "SELECT id, label FROM test ORDER BY id"))
27        printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
28
29    if (!is_object($res = mysqli_store_result($link, MYSQLI_STORE_RESULT_COPY_DATA)))
30        printf("[004] Expecting object, got %s/%s. [%d] %s\n",
31            gettype($res), $res, mysqli_errno($link), mysqli_error($link));
32
33    if (true !== ($tmp = mysqli_data_seek($res, 2)))
34        printf("[005] Expecting boolean/true, got %s/%s. [%d] %s\n",
35            gettype($tmp), $tmp, mysqli_errno($link), mysqli_error($link));
36
37    var_dump($res->fetch_assoc());
38
39    if (true !== ($tmp = mysqli_data_seek($res, 0)))
40        printf("[006] Expecting boolean/true, got %s/%s. [%d] %s\n",
41            gettype($tmp), $tmp, mysqli_errno($link), mysqli_error($link));
42
43    while ($row = $res->fetch_assoc()) {
44        printf("id = %d, label = %s\n", $row['id'], $row['label']);
45    }
46
47    mysqli_free_result($res);
48    mysqli_close($link);
49
50    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
51        printf("[007] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
52            $host, $user, $db, $port, $socket);
53    }
54
55
56    if (!$res = mysqli_real_query($link, "SELECT id, label FROM test ORDER BY id"))
57        printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
58
59    if (!is_object($res = mysqli_store_result($link, MYSQLI_STORE_RESULT_COPY_DATA)))
60        printf("[009] Expecting object, got %s/%s. [%d] %s\n",
61            gettype($res), $res, mysqli_errno($link), mysqli_error($link));
62
63    $no_result = 0;
64    for ($i = 0; $i < 1000; $i++) {
65        $idx = mt_rand(-100, 100);
66        if (true === @mysqli_data_seek($res, $idx)) {
67            $row = $res->fetch_assoc();
68            if (!isset($row['id']) || !isset($row['label'])) {
69                printf("[010] Brute force seek %d returned %d\n", $idx, var_export($row, true));
70            }
71        } else {
72            $no_result++;
73        }
74    }
75    printf("No result: %d\n", $no_result);
76
77    /* implicit free, implicit store */
78    /* meta and fetch lengths code */
79    if (!$res = mysqli_query($link, "SELECT CONCAT(id, id) AS _c, label FROM test ORDER BY id DESC LIMIT 2"))
80        printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
81
82    printf("Default\n");
83    var_dump(mysqli_fetch_lengths($res));
84    $fields = $res->fetch_fields();
85    while ($row = $res->fetch_assoc()) {
86        var_dump(mysqli_fetch_lengths($res));
87    }
88    var_dump(mysqli_fetch_lengths($res));
89
90    if (!$res = mysqli_real_query($link, "SELECT CONCAT(id, id) AS _c, label FROM test ORDER BY id DESC LIMIT 2"))
91        printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
92
93    if (!is_object($res = mysqli_store_result($link, MYSQLI_STORE_RESULT_COPY_DATA)))
94        printf("[013] Expecting object, got %s/%s. [%d] %s\n",
95            gettype($res), $res, mysqli_errno($link), mysqli_error($link));
96
97    printf("Copy\n");
98    var_dump(mysqli_fetch_lengths($res));
99    $fields_copy = $res->fetch_fields();
100    while ($row = $res->fetch_assoc()) {
101        var_dump(mysqli_fetch_lengths($res));
102    }
103    var_dump(mysqli_fetch_lengths($res));
104
105    /* There's no need for in-depth testing here. If you want in-depth switch mysqlnd
106    globally to copy mode and run all the tests */
107    foreach ($fields as $k => $field_info) {
108        if ($fields_copy[$k] != $field_info) {
109            printf("[014] Metadata seems to differ, dumping\n");
110            var_dump($field_info);
111            var_dump($fields_copy[$k]);
112        }
113    }
114
115    /* fetch all */
116
117    if (!$res = mysqli_real_query($link, "SELECT id, label FROM test ORDER BY id DESC LIMIT 2"))
118        printf("[015] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
119
120    if (!is_object($res = mysqli_store_result($link, MYSQLI_STORE_RESULT_COPY_DATA)))
121        printf("[016] Expecting object, got %s/%s. [%d] %s\n",
122            gettype($res), $res, mysqli_errno($link), mysqli_error($link));
123
124    foreach (mysqli_fetch_all($res, MYSQLI_ASSOC) as $row) {
125        printf("id = %d label = %s\n", $row['id'], $row['label']);
126    }
127
128    if (!$res = mysqli_real_query($link, "SELECT id, label FROM test ORDER BY id DESC LIMIT 2"))
129        printf("[017] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
130
131    if (!is_object($res = mysqli_store_result($link, MYSQLI_STORE_RESULT_COPY_DATA)))
132        printf("[018] Expecting object, got %s/%s. [%d] %s\n",
133            gettype($res), $res, mysqli_errno($link), mysqli_error($link));
134
135    /* provoke out of sync */
136    if (!mysqli_real_query($link, "SELECT id, label FROM test ORDER BY id DESC LIMIT 2"))
137        printf("[019] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
138
139    var_dump($res->fetch_assoc());
140
141    if (!$res = mysqli_real_query($link, "SELECT id, label FROM test ORDER BY id ASC LIMIT 2"))
142        printf("[020] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
143
144    if (!is_object($res = mysqli_store_result($link, MYSQLI_STORE_RESULT_COPY_DATA)))
145        printf("[021] Expecting object, got %s/%s. [%d] %s\n",
146            gettype($res), $res, mysqli_errno($link), mysqli_error($link));
147
148    /* user conn killed, res associated with conn, fetch from res */
149    unset($link);
150    var_dump($res->fetch_assoc());
151
152
153    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
154        printf("[022] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
155            $host, $user, $db, $port, $socket);
156    }
157
158    if (!$res = mysqli_real_query($link, "INSERT INTO test(id, label) VALUES (100, 'z')"))
159        printf("[023] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
160
161    mysqli_store_result($link, MYSQLI_STORE_RESULT_COPY_DATA);
162
163    if (mysqli_get_server_version($link) > 50000) {
164        // let's try to play with stored procedures
165        mysqli_real_query($link, 'DROP PROCEDURE IF EXISTS p');
166        if (mysqli_real_query($link, 'CREATE PROCEDURE p(OUT ver_param VARCHAR(25)) READS SQL DATA BEGIN SELECT id FROM test WHERE id >= 100 ORDER BY id; SELECT id + 1, label FROM test WHERE id > 0 AND id < 3 ORDER BY id; SELECT VERSION() INTO ver_param;
167END;')) {
168            mysqli_multi_query($link, "CALL p(@version)");
169            do {
170                if ($res = $link->store_result(MYSQLI_STORE_RESULT_COPY_DATA)) {
171                    printf("---\n");
172                    var_dump($res->fetch_all());
173                    $res->free();
174                } else {
175                    if ($link->errno) {
176                        echo "Store failed: (" . $link->errno . ") " . $link->error;
177                    }
178                }
179            } while ($link->more_results() && $link->next_result());
180            mysqli_real_query($link, 'SELECT @version AS p_version');
181            $res = mysqli_store_result($link, MYSQLI_STORE_RESULT_COPY_DATA);
182
183            $tmp = mysqli_fetch_assoc($res);
184            if (!is_array($tmp) || empty($tmp) || !isset($tmp['p_version']) || ('' == $tmp['p_version'])) {
185                printf("[024] Expecting array [%d] %s\n", mysqli_errno($link), mysqli_error($link));
186                var_dump($tmp);
187            }
188
189            mysqli_free_result($res);
190        } else {
191                printf("[025] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
192        }
193    }
194
195    print "done!";
196?>
197--CLEAN--
198<?php
199    require_once("clean_table.inc");
200?>
201--EXPECTF--
202array(2) {
203  ["id"]=>
204  string(1) "3"
205  ["label"]=>
206  string(1) "c"
207}
208id = 1, label = a
209id = 2, label = b
210id = 3, label = c
211id = 4, label = d
212id = 5, label = e
213id = 6, label = f
214No result: %d
215Default
216bool(false)
217array(2) {
218  [0]=>
219  int(2)
220  [1]=>
221  int(1)
222}
223array(2) {
224  [0]=>
225  int(2)
226  [1]=>
227  int(1)
228}
229bool(false)
230Copy
231bool(false)
232array(2) {
233  [0]=>
234  int(2)
235  [1]=>
236  int(1)
237}
238array(2) {
239  [0]=>
240  int(2)
241  [1]=>
242  int(1)
243}
244bool(false)
245id = 6 label = f
246id = 5 label = e
247array(2) {
248  ["id"]=>
249  string(1) "6"
250  ["label"]=>
251  string(1) "f"
252}
253[020] [2014] %s
254array(2) {
255  ["id"]=>
256  string(1) "6"
257  ["label"]=>
258  string(1) "f"
259}
260---
261array(1) {
262  [0]=>
263  array(1) {
264    [0]=>
265    string(3) "100"
266  }
267}
268---
269array(2) {
270  [0]=>
271  array(2) {
272    [0]=>
273    string(1) "2"
274    [1]=>
275    string(1) "a"
276  }
277  [1]=>
278  array(2) {
279    [0]=>
280    string(1) "3"
281    [1]=>
282    string(1) "b"
283  }
284}
285done!
286