mysql_fetch_field.phpt 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. --TEST--
  2. mysql_fetch_field()
  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. // Note: no SQL type tests, internally the same function gets used as for mysql_fetch_array() which does a lot of SQL type test
  14. if (!is_null($tmp = @mysql_fetch_field()))
  15. printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  16. if (NULL !== ($tmp = @mysql_fetch_field($link)))
  17. printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  18. require('table.inc');
  19. $version = mysql_get_server_info($link);
  20. if (!preg_match('@(\d+)\.(\d+)\.(\d+)@ism', $version, $matches))
  21. printf("[003] Cannot get server version\n");
  22. $version = ($matches[1] * 100) + ($matches[2] * 10) + $matches[3];
  23. if (!$res = mysql_query("SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1", $link)) {
  24. printf("[004] [%d] %s\n", mysql_errno($link), mysql_error($link));
  25. }
  26. while ($tmp = mysql_fetch_field($res))
  27. var_dump($tmp);
  28. var_dump($tmp);
  29. mysql_free_result($res);
  30. if (!$res = mysql_query("SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1", $link)) {
  31. printf("[005] [%d] %s\n", mysql_errno($link), mysql_error($link));
  32. }
  33. if (false !== ($tmp = mysql_fetch_field($res, PHP_INT_MAX - 1)))
  34. printf("[006] Expecting boolean/false got %s/%s\n", gettype($tmp), var_export($tmp, true));
  35. mysql_free_result($res);
  36. if (false !== ($tmp = mysql_fetch_field($res)))
  37. printf("[007] Expecting boolean/false, got %s/%s\n", gettype($tmp), var_export($tmp, true));
  38. $types = array(
  39. 'BIT' => array(1, 'int'),
  40. 'TINYINT' => array(1, 'int'),
  41. 'BOOL' => array('true', 'int'),
  42. 'BOOL' => array(1, 'int'),
  43. 'SMALLINT' => array(32767, 'int'),
  44. 'MEDIUMINT' => array(8388607, 'int'),
  45. 'INT' => array(100, 'int'),
  46. 'BIGINT' => array(100, 'int'),
  47. 'FLOAT' => array(100, 'real'),
  48. 'DOUBLE' => array(100, 'real'),
  49. 'DECIMAL' => array(100, 'real'),
  50. 'DATE' => array(@date('Y-m-d'), 'date'),
  51. 'DATETIME' => array(@date('Y-m-d H:i:s'), 'datetime'),
  52. 'TIMESTAMP' => array(@date('Y-m-d H:i:s'), 'timestamp'),
  53. 'TIME' => array(@date('H:i:s'), 'time'),
  54. 'YEAR' => array(@date('Y'), 'year'),
  55. 'CHAR(1)' => array('a', 'string'),
  56. 'VARCHAR(1)' => array('a', 'string'),
  57. 'BINARY(1)' => array('a', 'string'),
  58. 'VARBINARY(1)' => array('a', 'string'),
  59. 'TINYBLOB' => array('a', 'blob'),
  60. 'TINYTEXT' => array('a', 'blob'),
  61. 'BLOB' => array('a', 'blob'),
  62. 'TEXT' => array('a', 'blob'),
  63. 'MEDIUMBLOB' => array('a', 'blob'),
  64. 'MEDIUMTEXT' => array('a', 'blob'),
  65. 'LONGBLOB' => array('a', 'blob'),
  66. 'LONGTEXT' => array('a', 'blob'),
  67. 'ENUM("a", "b")' => array('a', 'string'), /* !!! */
  68. 'SET("a", "b")' => array('a', 'string'), /* !!! */
  69. );
  70. foreach ($types as $type_name => $type_desc) {
  71. if (!mysql_query("DROP TABLE IF EXISTS test", $link))
  72. printf("[008/%s] [%d] %s\n", $type_name, mysql_errno($link), mysql_error($link));
  73. if (!mysql_query(sprintf("CREATE TABLE test(id INT, label %s) ENGINE = %s", $type_name, $engine), $link)) {
  74. // server and/or engine might not support the data type
  75. continue;
  76. }
  77. if (is_string($type_desc[0]))
  78. $insert = sprintf("INSERT INTO test(id, label) VALUES (1, '%s')", $type_desc[0]);
  79. else
  80. $insert = sprintf("INSERT INTO test(id, label) VALUES (1, %s)", $type_desc[0]);
  81. if (!mysql_query($insert, $link)) {
  82. if (1366 == mysql_errno($link)) {
  83. /* Strict SQL mode - 1366, Incorrect integer value: 'true' for column 'label' at row 1 */
  84. continue;
  85. }
  86. printf("[009/%s] [%d] %s\n", $type_name, mysql_errno($link), mysql_error($link));
  87. continue;
  88. }
  89. if (!$res = mysql_query("SELECT id, label FROM test", $link)) {
  90. printf("[010/%s] [%d] %s\n", $type_name, mysql_errno($link), mysql_error($link));
  91. continue;
  92. }
  93. if (!$tmp = mysql_fetch_field($res, 1)) {
  94. printf("[011/%s] [%d] %s\n", $type_name, mysql_errno($link), mysql_error($link));
  95. }
  96. if ($type_desc[1] != $tmp->type) {
  97. printf("[012/%s] Expecting type '%s' got '%s'\n", $type_name, $type_desc[1], $tmp->type);
  98. }
  99. mysql_free_result($res);
  100. }
  101. if (!mysql_query("DROP TABLE IF EXISTS test", $link))
  102. printf("[013] [%d] %s\n", mysql_errno($link), mysql_error($link));
  103. if (!mysql_query("CREATE TABLE test(id INT DEFAULT 1)"))
  104. printf("[014] [%d] %s\n", mysql_errno($link), mysql_error($link));
  105. if (!mysql_query("INSERT INTO test(id) VALUES (2)"))
  106. printf("[015] [%d] %s\n", mysql_errno($link), mysql_error($link));
  107. if (!$res = mysql_query("SELECT id FROM test", $link)) {
  108. printf("[016] [%d] %s\n", mysql_errno($link), mysql_error($link));
  109. }
  110. var_dump(mysql_fetch_field($res));
  111. mysql_free_result($res);
  112. if (!$res = mysql_query("SELECT id FROM test", $link)) {
  113. printf("[017] [%d] %s\n", mysql_errno($link), mysql_error($link));
  114. }
  115. $res = mysql_list_fields($db, 'test');
  116. $found = false;
  117. while ($tmp = mysql_fetch_field($res)) {
  118. if ($tmp->name == 'id') {
  119. printf("Fetch field from mysql_list_fields result set.\n");
  120. $found = true;
  121. var_dump($tmp);
  122. }
  123. }
  124. if (!$found)
  125. printf("[018] mysqli_list_fields result set processing has failed.\n");
  126. mysql_free_result($res);
  127. mysql_close($link);
  128. print "done!";
  129. ?>
  130. --CLEAN--
  131. <?php
  132. require_once("clean_table.inc");
  133. ?>
  134. --EXPECTF--
  135. 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
  136. object(stdClass)#%d (13) {
  137. [%u|b%"name"]=>
  138. %unicode|string%(2) "ID"
  139. [%u|b%"table"]=>
  140. %unicode|string%(4) "TEST"
  141. [%u|b%"def"]=>
  142. %unicode|string%(0) ""
  143. [%u|b%"max_length"]=>
  144. int(1)
  145. [%u|b%"not_null"]=>
  146. int(1)
  147. [%u|b%"primary_key"]=>
  148. int(1)
  149. [%u|b%"multiple_key"]=>
  150. int(0)
  151. [%u|b%"unique_key"]=>
  152. int(0)
  153. [%u|b%"numeric"]=>
  154. int(1)
  155. [%u|b%"blob"]=>
  156. int(0)
  157. [%u|b%"type"]=>
  158. %unicode|string%(3) "int"
  159. [%u|b%"unsigned"]=>
  160. int(0)
  161. [%u|b%"zerofill"]=>
  162. int(0)
  163. }
  164. object(stdClass)#%d (13) {
  165. [%u|b%"name"]=>
  166. %unicode|string%(5) "label"
  167. [%u|b%"table"]=>
  168. %unicode|string%(4) "TEST"
  169. [%u|b%"def"]=>
  170. %unicode|string%(0) ""
  171. [%u|b%"max_length"]=>
  172. int(1)
  173. [%u|b%"not_null"]=>
  174. int(0)
  175. [%u|b%"primary_key"]=>
  176. int(0)
  177. [%u|b%"multiple_key"]=>
  178. int(0)
  179. [%u|b%"unique_key"]=>
  180. int(0)
  181. [%u|b%"numeric"]=>
  182. int(0)
  183. [%u|b%"blob"]=>
  184. int(0)
  185. [%u|b%"type"]=>
  186. %unicode|string%(6) "string"
  187. [%u|b%"unsigned"]=>
  188. int(0)
  189. [%u|b%"zerofill"]=>
  190. int(0)
  191. }
  192. bool(false)
  193. Warning: mysql_fetch_field(): Bad field offset in %s on line %d
  194. Warning: mysql_fetch_field(): %d is not a valid MySQL result resource in %s on line %d
  195. object(stdClass)#%d (13) {
  196. [%u|b%"name"]=>
  197. %unicode|string%(2) "id"
  198. [%u|b%"table"]=>
  199. %unicode|string%(4) "test"
  200. [%u|b%"def"]=>
  201. %unicode|string%(0) ""
  202. [%u|b%"max_length"]=>
  203. int(1)
  204. [%u|b%"not_null"]=>
  205. int(0)
  206. [%u|b%"primary_key"]=>
  207. int(0)
  208. [%u|b%"multiple_key"]=>
  209. int(0)
  210. [%u|b%"unique_key"]=>
  211. int(0)
  212. [%u|b%"numeric"]=>
  213. int(1)
  214. [%u|b%"blob"]=>
  215. int(0)
  216. [%u|b%"type"]=>
  217. %unicode|string%(3) "int"
  218. [%u|b%"unsigned"]=>
  219. int(0)
  220. [%u|b%"zerofill"]=>
  221. int(0)
  222. }
  223. Fetch field from mysql_list_fields result set.
  224. object(stdClass)#%d (13) {
  225. [%u|b%"name"]=>
  226. %unicode|string%(2) "id"
  227. [%u|b%"table"]=>
  228. %unicode|string%(4) "test"
  229. [%u|b%"def"]=>
  230. %unicode|string%(1) "1"
  231. [%u|b%"max_length"]=>
  232. int(0)
  233. [%u|b%"not_null"]=>
  234. int(0)
  235. [%u|b%"primary_key"]=>
  236. int(0)
  237. [%u|b%"multiple_key"]=>
  238. int(0)
  239. [%u|b%"unique_key"]=>
  240. int(0)
  241. [%u|b%"numeric"]=>
  242. int(1)
  243. [%u|b%"blob"]=>
  244. int(0)
  245. [%u|b%"type"]=>
  246. %unicode|string%(3) "int"
  247. [%u|b%"unsigned"]=>
  248. int(0)
  249. [%u|b%"zerofill"]=>
  250. int(0)
  251. }
  252. done!