mysqli_fetch_assoc_zerofill.phpt 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. --TEST--
  2. mysqli_fetch_assoc() - ZEROFILL
  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. require_once('table.inc');
  13. function zerofill($offset, $link, $datatype, $insert = 1) {
  14. mysqli_query($link, 'ALTER TABLE test DROP zero');
  15. $sql = sprintf('ALTER TABLE test ADD zero %s UNSIGNED ZEROFILL', $datatype);
  16. if (!mysqli_query($link, $sql)) {
  17. // no worries - server might not support it
  18. return true;
  19. }
  20. if (!mysqli_query($link, sprintf('UPDATE test SET zero = %s', $insert))) {
  21. printf("[%03d] UPDATE failed, [%d] %s\n",
  22. $offset, mysqli_errno($link), mysqli_error($link));
  23. return false;
  24. }
  25. if (!($res = mysqli_query($link, 'SELECT zero FROM test LIMIT 1'))) {
  26. printf("[%03d] SELECT failed, [%d] %s\n",
  27. $offset, mysqli_errno($link), mysqli_error($link));
  28. return false;
  29. }
  30. $row = mysqli_fetch_assoc($res);
  31. $meta = mysqli_fetch_fields($res);
  32. mysqli_free_result($res);
  33. $meta = $meta[0];
  34. $length = $meta->length;
  35. if ($length > strlen($insert)) {
  36. $expected = str_repeat('0', $length - strlen($insert));
  37. $expected .= $insert;
  38. if ($expected !== $row['zero']) {
  39. printf("[%03d] Expecting '%s' got '%s'\n", $offset, $expected, $row['zero']);
  40. return false;
  41. }
  42. } else if ($length <= 1) {
  43. printf("[%03d] Length reported is too small to run test\n", $offset);
  44. return false;
  45. }
  46. return true;
  47. }
  48. zerofill(2, $link, 'TINYINT');
  49. zerofill(3, $link, 'SMALLINT');
  50. zerofill(4, $link, 'MEDIUMINT');
  51. zerofill(5, $link, 'INT');
  52. zerofill(6, $link, 'INTEGER');
  53. zerofill(7, $link, 'BIGINT');
  54. zerofill(8, $link, 'FLOAT');
  55. zerofill(9, $link, 'DOUBLE');
  56. zerofill(10, $link, 'DOUBLE PRECISION');
  57. zerofill(11, $link, 'DECIMAL');
  58. zerofill(12, $link, 'DEC');
  59. mysqli_close($link);
  60. print "done!";
  61. ?>
  62. --CLEAN--
  63. <?php
  64. require_once("clean_table.inc");
  65. ?>
  66. --EXPECT--
  67. done!