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