mysqli_fetch_object.phpt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. --TEST--
  2. mysqli_fetch_object()
  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. include_once("connect.inc");
  12. set_error_handler('handle_catchable_fatal');
  13. $tmp = NULL;
  14. $link = NULL;
  15. if (!is_null($tmp = @mysqli_fetch_object()))
  16. printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  17. if (!is_null($tmp = @mysqli_fetch_object($link)))
  18. printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  19. require('table.inc');
  20. if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 5")) {
  21. printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  22. }
  23. $obj = mysqli_fetch_object($res);
  24. if (($obj->ID !== "1") || ($obj->label !== "a") || (get_class($obj) != 'stdClass')) {
  25. printf("[004] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  26. var_dump($obj);
  27. }
  28. class mysqli_fetch_object_test {
  29. public $a = null;
  30. public $b = null;
  31. public function toString() {
  32. var_dump($this);
  33. }
  34. }
  35. $obj = mysqli_fetch_object($res, 'mysqli_fetch_object_test');
  36. if (($obj->ID !== "2") || ($obj->label !== "b") || ($obj->a !== NULL) || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_test')) {
  37. printf("[005] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  38. var_dump($obj);
  39. }
  40. class mysqli_fetch_object_construct extends mysqli_fetch_object_test {
  41. public function __construct($a, $b) {
  42. $this->a = $a;
  43. $this->b = $b;
  44. }
  45. }
  46. try {
  47. $obj = mysqli_fetch_object($res, 'mysqli_fetch_object_construct', array());
  48. if (($obj->ID !== "3") || ($obj->label !== "c") || ($obj->a !== NULL) || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_construct')) {
  49. printf("[006] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  50. var_dump($obj);
  51. }
  52. } catch (Throwable $e) {
  53. echo "Exception: " . $e->getMessage() . "\n";
  54. }
  55. try {
  56. $obj = mysqli_fetch_object($res, 'mysqli_fetch_object_construct', array('a'));
  57. if (($obj->ID !== "4") || ($obj->label !== "d") || ($obj->a !== 'a') || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_construct')) {
  58. printf("[007] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  59. var_dump($obj);
  60. }
  61. } catch (Throwable $e) {
  62. echo "Exception: " . $e->getMessage() . "\n";
  63. }
  64. $obj = mysqli_fetch_object($res, 'mysqli_fetch_object_construct', array('a', 'b'));
  65. if (($obj->ID !== "5") || ($obj->label !== "e") || ($obj->a !== 'a') || ($obj->b !== 'b') || (get_class($obj) != 'mysqli_fetch_object_construct')) {
  66. printf("[008] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  67. var_dump($obj);
  68. }
  69. var_dump(mysqli_fetch_object($res, 'mysqli_fetch_object_construct', array('a', 'b', 'c')));
  70. var_dump(mysqli_fetch_object($res));
  71. mysqli_free_result($res);
  72. if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST")) {
  73. printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  74. }
  75. mysqli_free_result($res);
  76. var_dump(mysqli_fetch_object($res));
  77. if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 5"))
  78. printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  79. /*
  80. TODO
  81. I'm using the procedural interface, this should not throw an exception.
  82. Also, I did not ask to get exceptions using the mysqli_options()
  83. */
  84. try {
  85. if (false !== ($obj = @mysqli_fetch_object($res, 'mysqli_fetch_object_construct', 'a')))
  86. printf("[011] Should have failed\n");
  87. } catch (Error $e) {
  88. handle_catchable_fatal($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
  89. }
  90. mysqli_free_result($res);
  91. if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 5"))
  92. printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  93. class mysqli_fetch_object_private_constructor extends mysqli_fetch_object_test {
  94. private function __construct($a, $b) {
  95. $this->a = $a;
  96. $this->b = $b;
  97. }
  98. }
  99. /*
  100. TODO
  101. I think we should bail out here. The following line will give a Fatal error: Call to private ... from invalid context
  102. var_dump($obj = new mysqli_fetch_object_private_constructor(1, 2));
  103. This does not fail.
  104. */
  105. $obj = mysqli_fetch_object($res, 'mysqli_fetch_object_private_constructor', array('a', 'b'));
  106. mysqli_free_result($res);
  107. // Fatal error, script execution will end
  108. var_dump(mysqli_fetch_object($res, 'this_class_does_not_exist'));
  109. mysqli_close($link);
  110. print "done!";
  111. ?>
  112. --CLEAN--
  113. <?php
  114. require_once("clean_table.inc");
  115. ?>
  116. --EXPECTF--
  117. [E_WARNING] mysqli_fetch_object() expects at least 1 parameter, 0 given in %s on line %d
  118. [E_WARNING] mysqli_fetch_object() expects parameter 1 to be mysqli_result, null given in %s on line %d
  119. Exception: Too few arguments to function mysqli_fetch_object_construct::__construct(), 0 passed and exactly 2 expected
  120. Exception: Too few arguments to function mysqli_fetch_object_construct::__construct(), 1 passed and exactly 2 expected
  121. NULL
  122. NULL
  123. [E_WARNING] mysqli_fetch_object(): Couldn't fetch mysqli_result in %s on line %d
  124. bool(false)
  125. [0] Argument 3 passed to mysqli_fetch_object() must be of the type array, string given in %s on line %d
  126. Fatal error: Class 'this_class_does_not_exist' not found in %s on line %d