summaryrefslogtreecommitdiff
path: root/ext/mysqli/tests/mysqli_multi_query.phpt
blob: 0cc260a55793584e3865b7bc8c910ffef78b40ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
--TEST--
mysqli_multi_query()
--SKIPIF--
<?php
require_once('skipif.inc');
require_once('skipifconnectfailure.inc');
?>
--FILE--
<?php
    require_once("connect.inc");

    require('table.inc');

    if (false !== ($tmp = mysqli_multi_query($link, "")))
        printf("[003] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);

    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"))
        printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));

    $i = 0;
    do {
        $res = mysqli_store_result($link);
        while ($row = mysqli_fetch_array($res))
            ;
        mysqli_free_result($res);
        $i++;
    } while (mysqli_next_result($link));

    printf("[006] %d\n", $i);

    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"))
        printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));

    $i = 0;
    while (mysqli_next_result($link) && ($res = mysqli_store_result($link))) {

        while ($row = mysqli_fetch_array($res))
            ;
        mysqli_free_result($res);
        printf("%d/%d\n", $i, mysqli_insert_id($link));
        $i++;
    }
    printf("[008] %d\n", $i);

    if (!mysqli_multi_query($link, "SELECT id, label FROM test"))
        printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));

    $i = 0;
    while (mysqli_next_result($link) && ($res = mysqli_store_result($link))) {
        while ($row = mysqli_fetch_array($res))
            $i++;
        mysqli_free_result($res);
    }
    printf("[010] %d\n", $i);

    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"))
        printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));

    $res_num = 1;
    do {
        if (!$res = mysqli_store_result($link)) {
            printf("[012 - %d] [%d] %s\n", $res_num, mysqli_errno($link), mysqli_error($link));
            continue;
        }

        $num_rows = 0;
        while ($row = mysqli_fetch_array($res)) {

            $num_rows++;
            if ($row['num'] != $res_num)
                printf("[013 - %d] Expecting %s got %s\n", $res_num, $res_num, $row['num']);
            if ($row['somechar'] != "a")
                printf("[014 - %d] Expecting a got %s\n", $res_num, $row['somechar']);

            if (1 == $num_rows) {
                /* simple metadata check */
                if (!($lengths = mysqli_fetch_lengths($res)))
                    printf("[015 - %d] [%d] %s\n", $res_num, mysqli_errno($link), mysqli_error($link));

                if (count($lengths) != 2)
                    printf("[016 - %d] Expecting 2 column lengths got %d [%d] %s\n", $res_num, count($lengths));

                foreach ($lengths as $k => $length)
                    if ($length <= 0)
                        printf("[017 - %d] Strange column lengths for column %d, got %d expecting any > 0\n",
                            $res_num, $k, $length);
                }
        }

        if ($num_rows != 1)
            printf("[018 - %d] Expecting 1 row, got %d rows\n", $num_rows);

        $res_num++;

        mysqli_free_result($res);

    } while (mysqli_next_result($link));

    if ($res_num != 4)
        printf("[015] Expecting 3 result sets got %d result set[s]\n", $res_num);

    mysqli_close($link);

    try {
        mysqli_multi_query($link, "SELECT id, label FROM test");
    } catch (Error $exception) {
        echo $exception->getMessage() . "\n";
    }

    print "done!";
?>
--CLEAN--
<?php
    require_once("clean_table.inc");
?>
--EXPECT--
[006] 3
[008] 0
[009] [2014] Commands out of sync; you can't run this command now
[010] 7
mysqli object is already closed
done!