mysqli_get_charset.phpt 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. --TEST--
  2. mysqli_get_charset()
  3. --EXTENSIONS--
  4. mysqli
  5. --SKIPIF--
  6. <?php
  7. require_once('skipifconnectfailure.inc');
  8. if (!function_exists('mysqli_get_charset'))
  9. die("skip: function not available");
  10. ?>
  11. --FILE--
  12. <?php
  13. require_once("connect.inc");
  14. require('table.inc');
  15. if (!$res = mysqli_query($link, 'SELECT version() AS server_version'))
  16. printf("[004] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  17. $tmp = mysqli_fetch_assoc($res);
  18. mysqli_free_result($res);
  19. $version = explode('.', $tmp['server_version']);
  20. if (empty($version))
  21. printf("[005] Cannot determine server version, need MySQL Server 4.1+ for the test!\n");
  22. if ($version[0] <= 4 && $version[1] < 1)
  23. printf("[006] Need MySQL Server 4.1+ for the test!\n");
  24. if (!$res = mysqli_query($link, 'SELECT @@character_set_connection AS charset, @@collation_connection AS collation'))
  25. printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  26. $tmp = mysqli_fetch_assoc($res);
  27. mysqli_free_result($res);
  28. if (!($character_set_connection = $tmp['charset']) || !($collation_connection = $tmp['collation']))
  29. printf("[008] Cannot determine current character set and collation\n");
  30. if (!$res = mysqli_query($link, $sql = sprintf("SHOW CHARACTER SET LIKE '%s'", $character_set_connection)))
  31. printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  32. $tmp = mysqli_fetch_assoc($res);
  33. if (empty($tmp))
  34. printf("[010] Cannot fetch Maxlen and/or Comment, test will fail: $sql\n");
  35. $maxlen = (isset($tmp['Maxlen'])) ? $tmp['Maxlen'] : '';
  36. $comment = (isset($tmp['Description'])) ? $tmp['Description'] : '';
  37. if (!$res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $collation_connection)))
  38. printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  39. $tmp = mysqli_fetch_assoc($res);
  40. mysqli_free_result($res);
  41. if (!($id = $tmp['Id']))
  42. printf("[012] Cannot fetch Id/Number, test will fail\n");
  43. if (!$res = mysqli_query($link, sprintf("SHOW VARIABLES LIKE 'character_sets_dir'")))
  44. printf("[013] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  45. $tmp = mysqli_fetch_assoc($res);
  46. mysqli_free_result($res);
  47. if (!($character_sets_dir = $tmp['Value']))
  48. printf("[014] Cannot fetch character_sets_dir, test will fail\n");
  49. if (!is_object($charset = mysqli_get_charset($link)))
  50. printf("[015] Expecting object/std_class, got %s/%s\n", gettype($charset), $charset);
  51. if (!isset($charset->charset) ||
  52. !in_array(gettype($charset->charset), array("string", "unicode")) ||
  53. ($character_set_connection !== $charset->charset))
  54. printf("[016] Expecting string/%s, got %s/%s\n", $character_set_connection, gettype($charset->charset), $charset->charset);
  55. if (!isset($charset->collation) ||
  56. !in_array(gettype($charset->collation), array("string", "unicode")) ||
  57. ($collation_connection !== $charset->collation))
  58. printf("[017] Expecting string/%s, got %s/%s\n", $collation_connection, gettype($charset->collation), $charset->collation);
  59. if (!isset($charset->dir) ||
  60. !is_string($charset->dir))
  61. printf("[019] Expecting string - ideally %s*, got %s/%s\n", $character_sets_dir, gettype($charset->dir), $charset->dir);
  62. if (!isset($charset->min_length) ||
  63. !(is_int($charset->min_length)) ||
  64. ($charset->min_length < 0) ||
  65. ($charset->min_length > $charset->max_length))
  66. printf("[020] Expecting int between 0 ... %d, got %s/%s\n", $charset->max_length,
  67. gettype($charset->min_length), $charset->min_length);
  68. if (!isset($charset->number) ||
  69. !is_int($charset->number) ||
  70. ($charset->number !== (int)$id))
  71. printf("[021] Expecting int/%d, got %s/%s\n", $id, gettype($charset->number), $charset->number);
  72. if (!isset($charset->state) ||
  73. !is_int($charset->state))
  74. printf("[022] Expecting int/any, got %s/%s\n", gettype($charset->state), $charset->state);
  75. mysqli_close($link);
  76. try {
  77. mysqli_get_charset($link);
  78. } catch (Error $exception) {
  79. echo $exception->getMessage() . "\n";
  80. }
  81. print "done!";
  82. ?>
  83. --CLEAN--
  84. <?php
  85. require_once("clean_table.inc");
  86. ?>
  87. --EXPECT--
  88. mysqli object is already closed
  89. done!