1--TEST--
2mysqli_stmt_num_rows()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('skipifconnectfailure.inc');
8?>
9--FILE--
10<?php
11	require_once("connect.inc");
12
13	$tmp    = NULL;
14	$link   = NULL;
15
16	if (!is_null($tmp = @mysqli_stmt_num_rows()))
17		printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
18
19	if (!is_null($tmp = @mysqli_stmt_num_rows($link)))
20		printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
21
22	require('table.inc');
23
24	if (!$stmt = mysqli_stmt_init($link))
25		printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
26
27	function func_test_mysqli_stmt_num_rows($stmt, $query, $expected, $offset) {
28
29		if (!mysqli_stmt_prepare($stmt, $query)) {
30			printf("[%03d] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
31			return false;
32		}
33
34		if (!mysqli_stmt_execute($stmt)) {
35			printf("[%03d] [%d] %s\n", $offset + 1, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
36			return false;
37		}
38
39		if (!mysqli_stmt_store_result($stmt)) {
40			printf("[%03d] [%d] %s\n", $offset + 2, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
41			return false;
42		}
43
44		if ($expected !== ($tmp = mysqli_stmt_num_rows($stmt)))
45			printf("[%03d] Expecting %s/%d, got %s/%d\n", $offset + 3,
46				gettype($expected), $expected,
47				gettype($tmp), $tmp);
48
49		mysqli_stmt_free_result($stmt);
50
51		return true;
52	}
53
54	func_test_mysqli_stmt_num_rows($stmt, "SELECT 1 AS a", 1, 10);
55	func_test_mysqli_stmt_num_rows($stmt, "SHOW VARIABLES LIKE '%nixnutz%'", 0, 20);
56	// Note: for statements that return no result set mysqli_num_rows() differs from mysqli_stmt_num_rows() slightly
57	// mysqli_num_rows() failed to fetch the result set and the PHP parameter check makes it return NULL
58	// mysqli_stmt_numrows() has a valid resource to work on and it will return int/0 instead. No bug, but
59	// slightly different behaviour... - if you really check the data types and don't rely on casting like 98% of all PHP
60	// users do.
61	func_test_mysqli_stmt_num_rows($stmt, "INSERT INTO test(id, label) VALUES (100, 'z')", 0, 30);
62
63	if ($res = mysqli_query($link, 'SELECT COUNT(id) AS num FROM test')) {
64		$row = mysqli_fetch_assoc($res);
65		mysqli_free_result($res);
66		func_test_mysqli_stmt_num_rows($stmt, "SELECT id, label FROM test", (int)$row['num'], 40);
67	} else {
68		printf("[050] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
69	}
70
71	print "run_tests.php don't fool me with your 'ungreedy' expression '.+?'!\n";
72
73	if (!mysqli_stmt_prepare($stmt, 'SELECT id FROM test'))
74		printf("[051] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
75
76	if (mysqli_stmt_execute($stmt)) {
77
78		$i = 0;
79		do {
80			if (0 !== ($tmp = mysqli_stmt_num_rows($stmt)))
81				printf("[53 - %03d] Expecting int/0, got %s/%s\n", $i, gettype($tmp), $tmp);
82			$i++;
83		} while (mysqli_stmt_fetch($stmt));
84
85		/* NOTE to users
86		Behaviour with libmysql is UNDEFINED, see http://news.php.net/php.internals/55210
87		Because it is undefined it is allowed to the mysqlnd DEVELOPER to implement
88		any behaviour they like, including the one checked for in this test.
89		What the test does is cover an implementation detail of the mysqlnd library.
90		This implementation detail may, at any time, change without prior notice.
91		On the contrary, the mysqlnd way is a reasonable one and, maybe, one fine
92		day, after Klingons visited earh, becomes the official one. Meanwhile do
93		not rely on it.
94		*/
95		if ($IS_MYSQLND && (7 !== ($tmp = mysqli_stmt_num_rows($stmt))))
96			printf("[54] Expecting int/7, got %s/%s\n", gettype($tmp), $tmp);
97
98	} else {
99		printf("[055] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
100	}
101
102	mysqli_stmt_close($stmt);
103
104	if (NULL !== ($tmp = mysqli_stmt_num_rows($stmt)))
105		printf("[056] Expecting NULL, got %s/%s\n");
106
107	mysqli_close($link);
108	print "done!";
109?>
110--CLEAN--
111<?php
112	require_once("clean_table.inc");
113?>
114--EXPECTF--
115run_tests.php don't fool me with your 'ungreedy' expression '.+?'!
116
117Warning: mysqli_stmt_num_rows(): Couldn't fetch mysqli_stmt in %s on line %d
118done!