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