1--TEST--
2mysqli_reap_async_query()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('connect.inc');
8require_once('skipifconnectfailure.inc');
9
10if (!$IS_MYSQLND)
11	die("skip mysqlnd only feature, compile PHP using --with-mysqli=mysqlnd");
12?>
13--FILE--
14<?php
15	require_once('connect.inc');
16
17	function get_connection() {
18		global $host, $user, $passwd, $db, $port, $socket;
19
20		if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
21			printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
22		return $link;
23	}
24
25	if (!$link = get_connection())
26		printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
27
28	if (NULL !== ($tmp = @mysqli_reap_async_query()))
29		printf("[002] Expecting NULL got %s\n", var_export($tmp, true));
30
31	$l = array($link);
32	if (NULL !== ($tmp = @mysqli_reap_async_query($l)))
33		printf("[003] Expecting NULL got %s\n", var_export($tmp, true));
34
35	if (NULL !== ($tmp = @mysqli_reap_async_query($link, $link)))
36		printf("[004] Expecting NULL got %s\n", var_export($tmp, true));
37
38
39	function poll_async($offset, $link, $links, $errors, $reject, $exp_ready, $use_oo_syntax) {
40
41		if ($exp_ready !== ($tmp = mysqli_poll($links, $errors, $reject, 0, 2000)))
42			printf("[%03d + 1] There should be %d links ready to read from, %d ready\n",
43				$offset, $exp_ready, $tmp);
44
45		foreach ($links as $mysqli) {
46			if ($use_oo_syntax) {
47				$res = $mysqli->reap_async_query();
48			} else {
49				$res = mysqli_reap_async_query($mysqli);
50			}
51			if (is_object($res)) {
52				printf("[%03d + 2] %s\n", $offset, var_export($res->fetch_assoc(), true));
53			} else if (mysqli_errno($mysqli) > 0) {
54				printf("[%03d + 3] Error indicated through links array: %d/%s",
55					$offset, mysqli_errno($mysqli), mysqli_error($mysqli));
56			} else {
57				printf("[%03d + 4] Cannot fetch and no error set - non resultset query (no SELECT)!\n", $offset);
58			}
59		}
60
61		foreach ($errors as $mysqli)
62			printf("[%03d + 5] Error on %d: %d/%s\n",
63				$offset, mysqli_thread_id($mysqli), mysqli_errno($mysqli), mysqli_error($mysqli));
64
65		foreach ($reject as $mysqli)
66			printf("[%03d + 6] Rejecting thread %d: %d/%s\n",
67				$offset, mysqli_thread_id($mysqli), mysqli_errno($mysqli), mysqli_error($mysqli));
68
69	}
70
71	// Connections on which no query has been send - 1
72	$link = get_connection();
73	$link->query("SELECT 1 AS _one", MYSQLI_ASYNC | MYSQLI_STORE_RESULT);
74	$links = array($link);
75	$errors = array($link);
76	$reject = array($link);
77	poll_async(12, $link, $links, $errors, $reject, 1, false);
78	mysqli_close($link);
79
80	$link = get_connection();
81	$link->query("SELECT 2 AS _two", MYSQLI_ASYNC | MYSQLI_USE_RESULT);
82	$links = array($link);
83	$errors = array($link);
84	$reject = array($link);
85	poll_async(13, $link, $links, $errors, $reject, 1, true);
86	mysqli_close($link);
87
88	print "done!";
89?>
90--EXPECTF--
91[012 + 2] array (
92  '_one' => '1',
93)
94[013 + 2] array (
95  '_two' => '2',
96)
97done!
98