1--TEST--
2mysqli_multi_query()
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once('skipifconnectfailure.inc');
8?>
9--FILE--
10<?php
11    require_once("connect.inc");
12
13    require('table.inc');
14
15    if (false !== ($tmp = mysqli_multi_query($link, "")))
16        printf("[003] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
17
18    if (!mysqli_multi_query($link, "SELECT 1 AS a; SELECT 1 AS a, 2 AS b; SELECT id FROM test ORDER BY id LIMIT 3"))
19        printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
20
21    $i = 0;
22    do {
23        $res = mysqli_store_result($link);
24        while ($row = mysqli_fetch_array($res))
25            ;
26        mysqli_free_result($res);
27        $i++;
28    } while (mysqli_next_result($link));
29
30    printf("[006] %d\n", $i);
31
32    if (!mysqli_multi_query($link, "ALTER TABLE test MODIFY id INT AUTO_INCREMENT; INSERT INTO test(label) VALUES ('a'); SELECT id, label FROM test ORDER BY id"))
33        printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
34
35    $i = 0;
36    while (mysqli_next_result($link) && ($res = mysqli_store_result($link))) {
37
38        while ($row = mysqli_fetch_array($res))
39            ;
40        mysqli_free_result($res);
41        printf("%d/%d\n", $i, mysqli_insert_id($link));
42        $i++;
43    }
44    printf("[008] %d\n", $i);
45
46    if (!mysqli_multi_query($link, "SELECT id, label FROM test"))
47        printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
48
49    $i = 0;
50    while (mysqli_next_result($link) && ($res = mysqli_store_result($link))) {
51        while ($row = mysqli_fetch_array($res))
52            $i++;
53        mysqli_free_result($res);
54    }
55    printf("[010] %d\n", $i);
56
57    if (!mysqli_multi_query($link, "SELECT 1 AS num, 'a' AS somechar; SELECT 2 AS num, 'a' AS somechar; SELECT 3 AS num, 'a' AS somechar"))
58        printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
59
60    $res_num = 1;
61    do {
62        if (!$res = mysqli_store_result($link)) {
63            printf("[012 - %d] [%d] %s\n", $res_num, mysqli_errno($link), mysqli_error($link));
64            continue;
65        }
66
67        $num_rows = 0;
68        while ($row = mysqli_fetch_array($res)) {
69
70            $num_rows++;
71            if ($row['num'] != $res_num)
72                printf("[013 - %d] Expecting %s got %s\n", $res_num, $res_num, $row['num']);
73            if ($row['somechar'] != "a")
74                printf("[014 - %d] Expecting a got %s\n", $res_num, $row['somechar']);
75
76            if (1 == $num_rows) {
77                /* simple metadata check */
78                if (!($lengths = mysqli_fetch_lengths($res)))
79                    printf("[015 - %d] [%d] %s\n", $res_num, mysqli_errno($link), mysqli_error($link));
80
81                if (count($lengths) != 2)
82                    printf("[016 - %d] Expecting 2 column lengths got %d [%d] %s\n", $res_num, count($lengths));
83
84                foreach ($lengths as $k => $length)
85                    if ($length <= 0)
86                        printf("[017 - %d] Strange column lengths for column %d, got %d expecting any > 0\n",
87                            $res_num, $k, $length);
88                }
89        }
90
91        if ($num_rows != 1)
92            printf("[018 - %d] Expecting 1 row, got %d rows\n", $num_rows);
93
94        $res_num++;
95
96        mysqli_free_result($res);
97
98    } while (mysqli_next_result($link));
99
100    if ($res_num != 4)
101        printf("[015] Expecting 3 result sets got %d result set[s]\n", $res_num);
102
103    mysqli_close($link);
104
105    try {
106        mysqli_multi_query($link, "SELECT id, label FROM test");
107    } catch (Error $exception) {
108        echo $exception->getMessage() . "\n";
109    }
110
111    print "done!";
112?>
113--CLEAN--
114<?php
115    require_once("clean_table.inc");
116?>
117--EXPECT--
118[006] 3
119[008] 0
120[009] [2014] Commands out of sync; you can't run this command now
121[010] 7
122mysqli object is already closed
123done!
124