mysql_num_rows.phpt 2.5 KB

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