mysqli_num_rows.phpt 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. --TEST--
  2. mysqli_num_rows()
  3. --SKIPIF--
  4. <?php
  5. require_once('skipif.inc');
  6. require_once('skipifemb.inc');
  7. require_once('skipifconnectfailure.inc');
  8. ?>
  9. --FILE--
  10. <?php
  11. require_once("connect.inc");
  12. $tmp = NULL;
  13. $link = NULL;
  14. if (!is_null($tmp = @mysqli_num_rows()))
  15. printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  16. if (!is_null($tmp = @mysqli_num_rows($link)))
  17. printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  18. require('table.inc');
  19. function func_test_mysqli_num_rows($link, $query, $expected, $offset, $test_free = false) {
  20. if (!$res = mysqli_query($link, $query, MYSQLI_STORE_RESULT)) {
  21. printf("[%03d] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
  22. return;
  23. }
  24. if ($expected !== ($tmp = mysqli_num_rows($res)))
  25. printf("[%03d] Expecting %s/%d, got %s/%d\n", $offset + 1,
  26. gettype($expected), $expected,
  27. gettype($tmp), $tmp);
  28. mysqli_free_result($res);
  29. if ($test_free && (NULL !== ($tmp = mysqli_num_rows($res))))
  30. printf("[%03d] Expecting NULL, got %s/%s\n", $offset + 2, gettype($tmp), $tmp);
  31. }
  32. func_test_mysqli_num_rows($link, "SELECT 1 AS a", 1, 5);
  33. func_test_mysqli_num_rows($link, "SHOW VARIABLES LIKE '%nixnutz%'", 0, 10);
  34. func_test_mysqli_num_rows($link, "INSERT INTO test(id, label) VALUES (100, 'z')", NULL, 15);
  35. func_test_mysqli_num_rows($link, "SELECT id FROM test LIMIT 2", 2, 20, true);
  36. if ($res = mysqli_query($link, 'SELECT COUNT(id) AS num FROM test')) {
  37. $row = mysqli_fetch_assoc($res);
  38. mysqli_free_result($res);
  39. func_test_mysqli_num_rows($link, "SELECT id, label FROM test", (int)$row['num'], 25);
  40. } else {
  41. printf("[030] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  42. }
  43. print "run_tests.php don't fool me with your 'ungreedy' expression '.+?'!\n";
  44. if ($res = mysqli_query($link, 'SELECT id FROM test', MYSQLI_USE_RESULT)) {
  45. $row = mysqli_fetch_row($res);
  46. if (0 !== ($tmp = mysqli_num_rows($res)))
  47. printf("[031] Expecting int/0, got %s/%d\n", gettype($tmp), $tmp);
  48. mysqli_free_result($res);
  49. } else {
  50. printf("[032] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  51. }
  52. mysqli_close($link);
  53. print "done!";
  54. ?>
  55. --CLEAN--
  56. <?php
  57. require_once("clean_table.inc");
  58. ?>
  59. --EXPECTF--
  60. Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in %s on line %d
  61. Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given in %s on line %d
  62. Warning: mysqli_num_rows(): Couldn't fetch mysqli_result in %s on line %d
  63. run_tests.php don't fool me with your 'ungreedy' expression '.+?'!
  64. Warning: mysqli_num_rows(): Function cannot be used with MYSQL_USE_RESULT in %s on line %d
  65. done!