xref: /PHP-5.3/ext/mysql/tests/mysql_num_rows.phpt (revision b23baaf0)
1--TEST--
2mysql_num_rows()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7?>
8--FILE--
9<?php
10include "connect.inc";
11
12$tmp    = NULL;
13$link   = NULL;
14
15if (!is_null($tmp = @mysql_num_rows()))
16printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
17
18if (NULL !== ($tmp = @mysql_num_rows($link)))
19printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
20
21require('table.inc');
22
23function func_test_mysql_num_rows($link, $query, $expected, $offset, $test_free = false) {
24
25	if (!$res = mysql_query($query, $link)) {
26		printf("[%03d] [%d] %s\n", $offset, mysql_errno($link), mysql_error($link));
27		return;
28	}
29
30	if ($expected !== ($tmp = mysql_num_rows($res)))
31		printf("[%03d] Expecting %s/%d, got %s/%d\n", $offset + 1,
32			gettype($expected), $expected,
33			gettype($tmp), $tmp);
34
35	mysql_free_result($res);
36
37	if ($test_free && (false !== ($tmp = mysql_num_rows($res))))
38		printf("[%03d] Expecting boolean/false, got %s/%s\n", $offset + 2, gettype($tmp), $tmp);
39}
40
41func_test_mysql_num_rows($link, "SELECT 1 AS a", 1, 5);
42func_test_mysql_num_rows($link, "SHOW VARIABLES LIKE '%nixnutz%'", 0, 10);
43func_test_mysql_num_rows($link, "INSERT INTO test(id, label) VALUES (100, 'z')", NULL, 15);
44func_test_mysql_num_rows($link, "SELECT id FROM test LIMIT 2", 2, 20, true);
45
46if ($res = mysql_query('SELECT COUNT(id) AS num FROM test', $link)) {
47
48	$row = mysql_fetch_assoc($res);
49	mysql_free_result($res);
50
51	func_test_mysql_num_rows($link, "SELECT id, label FROM test", (int)$row['num'], 25);
52
53} else {
54	printf("[030] [%d] %s\n", mysql_errno($link), mysql_error($link));
55}
56
57if ($res = mysql_unbuffered_query('SELECT id, label FROM test')) {
58
59	if (0 != mysql_num_rows($res))
60		printf("[032] Expecting 0 rows got %d\n", mysql_num_rows($res));
61
62	$rows = 0;
63	while ($row = mysql_fetch_assoc($res))
64		$rows++;
65
66	if ($rows != mysql_num_rows($res))
67		printf("[033] Expecting %d rows got %d\n", $rows, mysql_num_rows($res));
68
69	mysql_free_result($res);
70} else {
71	printf("[034] [%d] %s\n", mysql_errno($link), mysql_error($link));
72}
73
74mysql_close($link);
75print "done!";
76?>
77--CLEAN--
78<?php
79require_once("clean_table.inc");
80?>
81--EXPECTF--
82Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in %s on line %d
83
84Warning: mysql_free_result() expects parameter 1 to be resource, boolean given in %s on line %d
85
86Warning: mysql_num_rows(): %d is not a valid MySQL result resource in %s on line %d
87done!
88