xref: /PHP-8.1/ext/mysqli/tests/067.phpt (revision b5a14e6c)
1--TEST--
2function test: nested selects (cursors)
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7    require_once('skipifconnectfailure.inc');
8    require_once("connect.inc");
9
10    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
11        die("skip Cannot connect to check required version");
12
13    /* skip cursor test for server versions < 50009 */
14    if (mysqli_get_server_version($link) < 50009) {
15            die(sprintf("skip Server doesn't support cursors (%s)",
16                    mysqli_get_server_version($link)));
17    }
18    mysqli_close($link);
19?>
20--FILE--
21<?php
22    function open_cursor($mysql, $query) {
23        if (!is_object($stmt = $mysql->prepare($query))) {
24            printf("[001] Cannot create statement object for '%s', [%d] %s\n",
25                    $query, $mysql->errno, $mysql->error);
26        }
27
28        $stmt->attr_set(MYSQLI_STMT_ATTR_CURSOR_TYPE, MYSQLI_CURSOR_TYPE_READ_ONLY);
29        return $stmt;
30    }
31
32    require_once("connect.inc");
33    $mysql = new my_mysqli($host, $user, $passwd, $db, $port, $socket);
34
35    if (mysqli_get_server_version($mysql) < 50009) {
36        /* we really want to skip it... */
37        die(var_dump(63));
38    }
39
40    $a = array();
41
42    for ($i=0;$i < 3; $i++) {
43        $mysql->query("DROP TABLE IF EXISTS cursor$i");
44        $mysql->query("CREATE TABLE cursor$i (a int not null) ENGINE=" . $engine);
45        $mysql->query("INSERT INTO cursor$i VALUES (1),(2),(3),(4),(5),(6)");
46        $stmt[$i] = open_cursor($mysql, "SELECT a FROM cursor$i");
47        $stmt[$i]->execute();
48        $stmt[$i]->bind_result($a[$i]);
49    }
50
51
52    $cnt = 0;
53    while ($stmt[0]->fetch()) {
54        $stmt[1]->fetch();
55        $stmt[2]->fetch();
56        $cnt += $a[0] + $a[1] + $a[2];
57    }
58
59    for ($i=0; $i < 3; $i++) {
60        $stmt[$i]->close();
61    }
62
63    $mysql->close();
64    var_dump($cnt);
65?>
66--CLEAN--
67<?php
68require_once("connect.inc");
69if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
70   printf("[c001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
71
72for ($i =0; $i < 3; $i++) {
73    if (!mysqli_query($link, sprintf("DROP TABLE IF EXISTS cursor%d", $i)))
74        printf("[c002] Cannot drop table, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
75}
76
77mysqli_close($link);
78?>
79--EXPECT--
80int(63)
81