xref: /PHP-7.2/ext/mysqli/tests/067.phpt (revision f1d7e3ca)
1--TEST--
2function test: nested selects (cursors)
3--SKIPIF--
4<?php
5	require_once('skipif.inc');
6	require_once('skipifconnectfailure.inc');
7	require_once("connect.inc");
8
9	if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
10		die("skip Cannot connect to check required version");
11
12	/* skip cursor test for versions < 50004 */
13	if ((!$IS_MYSQLND && (mysqli_get_client_version() < 50009)) ||
14			(mysqli_get_server_version($link) < 50009)) {
15			die(sprintf("skip Client library doesn't support cursors (%s/%s)",
16					mysqli_get_client_version(), 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 ((!$IS_MYSQLND && mysqli_get_client_version() < 50009) ||
36		(mysqli_get_server_version($mysql) < 50009)) {
37		/* we really want to skip it... */
38		die(var_dump(63));
39	}
40
41	$a = array();
42
43	for ($i=0;$i < 3; $i++) {
44		$mysql->query("DROP TABLE IF EXISTS cursor$i");
45		$mysql->query("CREATE TABLE cursor$i (a int not null) ENGINE=" . $engine);
46		$mysql->query("INSERT INTO cursor$i VALUES (1),(2),(3),(4),(5),(6)");
47		$stmt[$i] = open_cursor($mysql, "SELECT a FROM cursor$i");
48		$stmt[$i]->execute();
49		$stmt[$i]->bind_result($a[$i]);
50	}
51
52
53	$cnt = 0;
54	while ($stmt[0]->fetch()) {
55		$stmt[1]->fetch();
56		$stmt[2]->fetch();
57		$cnt += $a[0] + $a[1] + $a[2];
58	}
59
60	for ($i=0; $i < 3; $i++) {
61		$stmt[$i]->close();
62	}
63
64	$mysql->close();
65	var_dump($cnt);
66?>
67--CLEAN--
68<?php
69require_once("connect.inc");
70if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
71   printf("[c001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
72
73for ($i =0; $i < 3; $i++) {
74	if (!mysqli_query($link, sprintf("DROP TABLE IF EXISTS cursor%d", $i)))
75		printf("[c002] Cannot drop table, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
76}
77
78mysqli_close($link);
79?>
80--EXPECT--
81int(63)
82