mysqli_character_set.phpt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. --TEST--
  2. Fetching results from tables of different charsets.
  3. --EXTENSIONS--
  4. mysqli
  5. --SKIPIF--
  6. <?php
  7. require_once('skipifconnectfailure.inc');
  8. if (!function_exists('mysqli_set_charset')) {
  9. die('skip mysqli_set_charset() not available');
  10. }
  11. ?>
  12. --FILE--
  13. <?php
  14. require_once("connect.inc");
  15. $tmp = NULL;
  16. $link = NULL;
  17. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  18. printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
  19. $host, $user, $db, $port, $socket);
  20. if (!$res = mysqli_query($link, 'SELECT version() AS server_version'))
  21. printf("[002] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  22. $tmp = mysqli_fetch_assoc($res);
  23. mysqli_free_result($res);
  24. $version = explode('.', $tmp['server_version']);
  25. if (empty($version))
  26. printf("[003] Cannot determine server version, need MySQL Server 4.1+ for the test!\n");
  27. if ($version[0] <= 4 && $version[1] < 1)
  28. printf("[004] Need MySQL Server 4.1+ for the test!\n");
  29. if (!$res = mysqli_query($link, "SHOW CHARACTER SET"))
  30. printf("[005] Cannot get list of available character sets, [%d] %s\n",
  31. mysqli_errno($link), mysqli_error($link));
  32. $charsets = array();
  33. while ($row = mysqli_fetch_assoc($res))
  34. $charsets[] = $row;
  35. mysqli_free_result($res);
  36. foreach ($charsets as $charset) {
  37. $k = $charset['Charset'];
  38. /* The server currently 17.07.2007 can't handle data sent in ucs2 */
  39. /* The server currently 16.08.2010 can't handle data sent in utf16 and utf32 */
  40. /* The server currently 02.09.2011 can't handle data sent in utf16le */
  41. /* As of MySQL 8.0.28, `SHOW CHARACTER SET` contains utf8mb3, but that is not yet supported by mysqlnd */
  42. if ($charset['Charset'] == 'ucs2' || $charset['Charset'] == 'utf16' || $charset['Charset'] == 'utf32' || 'utf16le' == $charset['Charset'] || 'utf8mb3' == $charset['Charset']) {
  43. continue;
  44. }
  45. if (!mysqli_query($link, "DROP TABLE IF EXISTS test"))
  46. printf("[006 + %s] [%d] %s\n", $k, mysqli_errno($link), mysqli_error($link));
  47. $sql = sprintf("CREATE TABLE test(id INT, label CHAR(1)) CHARACTER SET '%s' ", $charset['Charset']);
  48. if (!mysqli_query($link, $sql)) {
  49. printf("[007 + %s] %s [%d] %s\n", $k, $sql, mysqli_errno($link), mysqli_error($link));
  50. continue;
  51. }
  52. if (!mysqli_set_charset($link, $charset['Charset'])) {
  53. printf("[008 + %s] [%d] %s\n", $k, mysqli_errno($link), mysqli_error($link));
  54. continue;
  55. }
  56. for ($i = 1; $i <= 3; $i++) {
  57. if (!mysqli_query($link, sprintf("INSERT INTO test (id, label) VALUES (%d, '%s')",
  58. $i, mysqli_real_escape_string($link, chr(ord("a") + $i)))))
  59. {
  60. var_dump($charset['Charset']);
  61. printf("[009 + %s] [%d] %s\n", $k, mysqli_errno($link), mysqli_error($link));
  62. continue;
  63. }
  64. }
  65. if (!$res = mysqli_query($link, "SELECT id, label FROM test"))
  66. printf("[010 + %s] [%d] %s\n", $k, mysqli_errno($link), mysqli_error($link));
  67. for ($i = 1; $i <= 3; $i++) {
  68. if (!$tmp = mysqli_fetch_assoc($res))
  69. printf("[011 + %s] [%d] %s\n", $k, mysqli_errno($link), mysqli_error($link));
  70. if ($tmp['id'] != $i)
  71. printf("[012 + %s] Expecting %d, got %s, [%d] %s\n", $k,
  72. $i, $tmp['id'],
  73. mysqli_errno($link), mysqli_error($link));
  74. if ($tmp['label'] != chr(ord("a") + $i))
  75. printf("[013 + %s] Expecting %d, got %s, [%d] %s\n", $k,
  76. chr(ord("a") + $i), $tmp['label'],
  77. mysqli_errno($link), mysqli_error($link));
  78. }
  79. mysqli_free_result($res);
  80. }
  81. mysqli_close($link);
  82. print "done!";
  83. ?>
  84. --CLEAN--
  85. <?php
  86. require_once("clean_table.inc");
  87. ?>
  88. --EXPECT--
  89. done!