mysqli_fetch_field.phpt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. --TEST--
  2. mysqli_fetch_field()
  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. // Note: no SQL type tests, internally the same function gets used as for mysqli_fetch_array() which does a lot of SQL type test
  15. if (!is_null($tmp = @mysqli_fetch_field()))
  16. printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  17. if (!is_null($tmp = @mysqli_fetch_field($link)))
  18. printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  19. require('table.inc');
  20. // Make sure that client, connection and result charsets are all the
  21. // same. Not sure whether this is strictly necessary.
  22. if (!mysqli_set_charset($link, 'utf8'))
  23. printf("[%d] %s\n", mysqli_errno($link), mysqli_errno($link));
  24. $charsetInfo = mysqli_get_charset($link);
  25. if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1")) {
  26. printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  27. }
  28. /* ID column, binary charset */
  29. $tmp = mysqli_fetch_field($res);
  30. var_dump($tmp);
  31. /* label column, result set charset */
  32. $tmp = mysqli_fetch_field($res);
  33. var_dump($tmp);
  34. if ($tmp->charsetnr != $charsetInfo->number) {
  35. printf("[004] Expecting charset %s/%d got %d\n",
  36. $charsetInfo->charset, $charsetInfo->number, $tmp->charsetnr);
  37. }
  38. if ($tmp->length != $charsetInfo->max_length) {
  39. printf("[005] Expecting length %d got %d\n",
  40. $charsetInfo->max_length, $tmp->max_length);
  41. }
  42. if ($tmp->db != $db) {
  43. printf("011] Expecting database '%s' got '%s'\n",
  44. $db, $tmp->db);
  45. }
  46. var_dump(mysqli_fetch_field($res));
  47. mysqli_free_result($res);
  48. // Read http://bugs.php.net/bug.php?id=42344 on defaults!
  49. if (NULL !== ($tmp = mysqli_fetch_field($res)))
  50. printf("[006] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  51. if (!mysqli_query($link, "DROP TABLE IF EXISTS test"))
  52. printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  53. if (!mysqli_query($link, "CREATE TABLE test(id INT NOT NULL DEFAULT 1)"))
  54. printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  55. if (!mysqli_query($link, "INSERT INTO test(id) VALUES (2)"))
  56. printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  57. if (!$res = mysqli_query($link, "SELECT id as _default_test FROM test")) {
  58. printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  59. }
  60. var_dump(mysqli_fetch_assoc($res));
  61. /* binary */
  62. var_dump(mysqli_fetch_field($res));
  63. mysqli_free_result($res);
  64. mysqli_close($link);
  65. print "done!";
  66. ?>
  67. --CLEAN--
  68. <?php
  69. require_once("clean_table.inc");
  70. ?>
  71. --EXPECTF--
  72. object(stdClass)#%d (13) {
  73. [%u|b%"name"]=>
  74. %unicode|string%(2) "ID"
  75. [%u|b%"orgname"]=>
  76. %unicode|string%(2) "id"
  77. [%u|b%"table"]=>
  78. %unicode|string%(4) "TEST"
  79. [%u|b%"orgtable"]=>
  80. %unicode|string%(%d) "%s"
  81. [%u|b%"def"]=>
  82. %unicode|string%(0) ""
  83. [%u|b%"db"]=>
  84. %unicode|string%(%d) "%s"
  85. [%u|b%"catalog"]=>
  86. %unicode|string%(%d) "%s"
  87. [%u|b%"max_length"]=>
  88. int(1)
  89. [%u|b%"length"]=>
  90. int(11)
  91. [%u|b%"charsetnr"]=>
  92. int(63)
  93. [%u|b%"flags"]=>
  94. int(49155)
  95. [%u|b%"type"]=>
  96. int(3)
  97. [%u|b%"decimals"]=>
  98. int(0)
  99. }
  100. object(stdClass)#%d (13) {
  101. [%u|b%"name"]=>
  102. %unicode|string%(5) "label"
  103. [%u|b%"orgname"]=>
  104. %unicode|string%(5) "label"
  105. [%u|b%"table"]=>
  106. %unicode|string%(4) "TEST"
  107. [%u|b%"orgtable"]=>
  108. %unicode|string%(%d) "%s"
  109. [%u|b%"def"]=>
  110. %unicode|string%(0) ""
  111. [%u|b%"db"]=>
  112. %unicode|string%(%d) "%s"
  113. [%u|b%"catalog"]=>
  114. %unicode|string%(%d) "%s"
  115. [%u|b%"max_length"]=>
  116. int(%d)
  117. [%u|b%"length"]=>
  118. int(%d)
  119. [%u|b%"charsetnr"]=>
  120. int(%d)
  121. [%u|b%"flags"]=>
  122. int(0)
  123. [%u|b%"type"]=>
  124. int(254)
  125. [%u|b%"decimals"]=>
  126. int(0)
  127. }
  128. bool(false)
  129. Warning: mysqli_fetch_field(): Couldn't fetch mysqli_result in %s on line %d
  130. array(1) {
  131. [%u|b%"_default_test"]=>
  132. %unicode|string%(1) "2"
  133. }
  134. object(stdClass)#%d (13) {
  135. [%u|b%"name"]=>
  136. %unicode|string%(13) "_default_test"
  137. [%u|b%"orgname"]=>
  138. %unicode|string%(2) "id"
  139. [%u|b%"table"]=>
  140. %unicode|string%(%d) "%s"
  141. [%u|b%"orgtable"]=>
  142. %unicode|string%(%d) "%s"
  143. [%u|b%"def"]=>
  144. %unicode|string%(0) ""
  145. [%u|b%"db"]=>
  146. %unicode|string%(%d) "%s"
  147. [%u|b%"catalog"]=>
  148. %unicode|string%(%d) "%s"
  149. [%u|b%"max_length"]=>
  150. int(1)
  151. [%u|b%"length"]=>
  152. int(11)
  153. [%u|b%"charsetnr"]=>
  154. int(63)
  155. [%u|b%"flags"]=>
  156. int(32769)
  157. [%u|b%"type"]=>
  158. int(3)
  159. [%u|b%"decimals"]=>
  160. int(0)
  161. }
  162. done!