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