PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/zend/bad/ext/mysql/tests/mysql_fetch_field.php

http://github.com/facebook/hiphop-php
PHP | 149 lines | 122 code | 24 blank | 3 comment | 27 complexity | dea136cafd9a2c6cae8c7323688cec9e MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?php
  2. include "connect.inc";
  3. $tmp = NULL;
  4. $link = NULL;
  5. // Note: no SQL type tests, internally the same function gets used as for mysql_fetch_array() which does a lot of SQL type test
  6. if (!is_null($tmp = @mysql_fetch_field()))
  7. printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  8. if (NULL !== ($tmp = @mysql_fetch_field($link)))
  9. printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  10. require('table.inc');
  11. $version = mysql_get_server_info($link);
  12. if (!preg_match('@(\d+)\.(\d+)\.(\d+)@ism', $version, $matches))
  13. printf("[003] Cannot get server version\n");
  14. $version = ($matches[1] * 100) + ($matches[2] * 10) + $matches[3];
  15. if (!$res = mysql_query("SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1", $link)) {
  16. printf("[004] [%d] %s\n", mysql_errno($link), mysql_error($link));
  17. }
  18. while ($tmp = mysql_fetch_field($res))
  19. var_dump($tmp);
  20. var_dump($tmp);
  21. mysql_free_result($res);
  22. if (!$res = mysql_query("SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1", $link)) {
  23. printf("[005] [%d] %s\n", mysql_errno($link), mysql_error($link));
  24. }
  25. if (false !== ($tmp = mysql_fetch_field($res, PHP_INT_MAX - 1)))
  26. printf("[006] Expecting boolean/false got %s/%s\n", gettype($tmp), var_export($tmp, true));
  27. mysql_free_result($res);
  28. if (false !== ($tmp = mysql_fetch_field($res)))
  29. printf("[007] Expecting boolean/false, got %s/%s\n", gettype($tmp), var_export($tmp, true));
  30. $types = array(
  31. 'BIT' => array(1, 'int'),
  32. 'TINYINT' => array(1, 'int'),
  33. 'BOOL' => array('true', 'int'),
  34. 'BOOL' => array(1, 'int'),
  35. 'SMALLINT' => array(32767, 'int'),
  36. 'MEDIUMINT' => array(8388607, 'int'),
  37. 'INT' => array(100, 'int'),
  38. 'BIGINT' => array(100, 'int'),
  39. 'FLOAT' => array(100, 'real'),
  40. 'DOUBLE' => array(100, 'real'),
  41. 'DECIMAL' => array(100, 'real'),
  42. 'DATE' => array(@date('Y-m-d'), 'date'),
  43. 'DATETIME' => array(@date('Y-m-d H:i:s'), 'datetime'),
  44. 'TIMESTAMP' => array(@date('Y-m-d H:i:s'), 'timestamp'),
  45. 'TIME' => array(@date('H:i:s'), 'time'),
  46. 'YEAR' => array(@date('Y'), 'year'),
  47. 'CHAR(1)' => array('a', 'string'),
  48. 'VARCHAR(1)' => array('a', 'string'),
  49. 'BINARY(1)' => array('a', 'string'),
  50. 'VARBINARY(1)' => array('a', 'string'),
  51. 'TINYBLOB' => array('a', 'blob'),
  52. 'TINYTEXT' => array('a', 'blob'),
  53. 'BLOB' => array('a', 'blob'),
  54. 'TEXT' => array('a', 'blob'),
  55. 'MEDIUMBLOB' => array('a', 'blob'),
  56. 'MEDIUMTEXT' => array('a', 'blob'),
  57. 'LONGBLOB' => array('a', 'blob'),
  58. 'LONGTEXT' => array('a', 'blob'),
  59. 'ENUM("a", "b")' => array('a', 'string'), /* !!! */
  60. 'SET("a", "b")' => array('a', 'string'), /* !!! */
  61. );
  62. foreach ($types as $type_name => $type_desc) {
  63. if (!mysql_query("DROP TABLE IF EXISTS test", $link))
  64. printf("[008/%s] [%d] %s\n", $type_name, mysql_errno($link), mysql_error($link));
  65. if (!mysql_query(sprintf("CREATE TABLE test(id INT, label %s) ENGINE = %s", $type_name, $engine), $link)) {
  66. // server and/or engine might not support the data type
  67. continue;
  68. }
  69. if (is_string($type_desc[0]))
  70. $insert = sprintf("INSERT INTO test(id, label) VALUES (1, '%s')", $type_desc[0]);
  71. else
  72. $insert = sprintf("INSERT INTO test(id, label) VALUES (1, %s)", $type_desc[0]);
  73. if (!mysql_query($insert, $link)) {
  74. if (1366 == mysql_errno($link)) {
  75. /* Strict SQL mode - 1366, Incorrect integer value: 'true' for column 'label' at row 1 */
  76. continue;
  77. }
  78. printf("[009/%s] [%d] %s\n", $type_name, mysql_errno($link), mysql_error($link));
  79. continue;
  80. }
  81. if (!$res = mysql_query("SELECT id, label FROM test", $link)) {
  82. printf("[010/%s] [%d] %s\n", $type_name, mysql_errno($link), mysql_error($link));
  83. continue;
  84. }
  85. if (!$tmp = mysql_fetch_field($res, 1)) {
  86. printf("[011/%s] [%d] %s\n", $type_name, mysql_errno($link), mysql_error($link));
  87. }
  88. if ($type_desc[1] != $tmp->type) {
  89. printf("[012/%s] Expecting type '%s' got '%s'\n", $type_name, $type_desc[1], $tmp->type);
  90. }
  91. mysql_free_result($res);
  92. }
  93. if (!mysql_query("DROP TABLE IF EXISTS test", $link))
  94. printf("[013] [%d] %s\n", mysql_errno($link), mysql_error($link));
  95. if (!mysql_query("CREATE TABLE test(id INT DEFAULT 1)"))
  96. printf("[014] [%d] %s\n", mysql_errno($link), mysql_error($link));
  97. if (!mysql_query("INSERT INTO test(id) VALUES (2)"))
  98. printf("[015] [%d] %s\n", mysql_errno($link), mysql_error($link));
  99. if (!$res = mysql_query("SELECT id FROM test", $link)) {
  100. printf("[016] [%d] %s\n", mysql_errno($link), mysql_error($link));
  101. }
  102. var_dump(mysql_fetch_field($res));
  103. mysql_free_result($res);
  104. if (!$res = mysql_query("SELECT id FROM test", $link)) {
  105. printf("[017] [%d] %s\n", mysql_errno($link), mysql_error($link));
  106. }
  107. $res = mysql_list_fields($db, 'test');
  108. $found = false;
  109. while ($tmp = mysql_fetch_field($res)) {
  110. if ($tmp->name == 'id') {
  111. printf("Fetch field from mysql_list_fields result set.\n");
  112. $found = true;
  113. var_dump($tmp);
  114. }
  115. }
  116. if (!$found)
  117. printf("[018] mysqli_list_fields result set processing has failed.\n");
  118. mysql_free_result($res);
  119. mysql_close($link);
  120. print "done!";
  121. ?>
  122. <?php error_reporting(0); ?>
  123. <?php
  124. require_once("clean_table.inc");
  125. ?>