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