mysqli_stmt_num_rows.phpt 4.0 KB

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