mysqli_set_charset.phpt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. --TEST--
  2. mysqli_set_charset()
  3. --EXTENSIONS--
  4. mysqli
  5. --SKIPIF--
  6. <?php
  7. require_once('skipifconnectfailure.inc');
  8. if (!function_exists('mysqli_set_charset'))
  9. die("skip Function not available");
  10. require_once('connect.inc');
  11. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  12. die(sprintf("skip Cannot connect, [%d] %s", mysqli_connect_errno(), mysqli_connect_error()));
  13. if (!($res = mysqli_query($link, 'SELECT version() AS server_version')) ||
  14. !($tmp = mysqli_fetch_assoc($res))) {
  15. mysqli_close($link);
  16. die(sprintf("skip Cannot check server version, [%d] %s\n",
  17. mysqli_errno($link), mysqli_error($link)));
  18. }
  19. mysqli_free_result($res);
  20. $version = explode('.', $tmp['server_version']);
  21. if (empty($version)) {
  22. mysqli_close($link);
  23. die(sprintf("skip Cannot check server version, based on '%s'",
  24. $tmp['server_version']));
  25. }
  26. if ($version[0] <= 4 && $version[1] < 1) {
  27. mysqli_close($link);
  28. die(sprintf("skip Requires MySQL Server 4.1+\n"));
  29. }
  30. if ((($res = mysqli_query($link, 'SHOW CHARACTER SET LIKE "latin1"', MYSQLI_STORE_RESULT)) &&
  31. (mysqli_num_rows($res) == 1)) ||
  32. (($res = mysqli_query($link, 'SHOW CHARACTER SET LIKE "latin2"', MYSQLI_STORE_RESULT)) &&
  33. (mysqli_num_rows($res) == 1))
  34. ) {
  35. // ok, required latin1 or latin2 are available
  36. mysqli_close($link);
  37. } else {
  38. die(sprintf("skip Requires character set latin1 or latin2\n"));
  39. mysqli_close($link);
  40. }
  41. ?>
  42. --FILE--
  43. <?php
  44. require_once("connect.inc");
  45. require('table.inc');
  46. if (!$res = mysqli_query($link, 'SELECT @@character_set_connection AS charset, @@collation_connection AS collation'))
  47. printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  48. $tmp = mysqli_fetch_assoc($res);
  49. mysqli_free_result($res);
  50. if (!$character_set_connection = $tmp['charset'])
  51. printf("[008] Cannot determine current character set and collation\n");
  52. $new_charset = ('latin1' == $character_set_connection) ? 'latin2' : 'latin1';
  53. if (!$res = mysqli_query($link, sprintf('SHOW CHARACTER SET LIKE "%s"', $new_charset), MYSQLI_STORE_RESULT))
  54. printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  55. if (mysqli_num_rows($res) == 0)
  56. printf("[010] Test will fail, because alternative test character set '%s' seems not supported\n", $new_charset);
  57. if (false !== ($ret = mysqli_set_charset($link, "this is not a valid character set")))
  58. printf("[011] Expecting boolean/false because of invalid character set, got %s/%s\n", gettype($ret), $ret);
  59. mysqli_close($link);
  60. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  61. printf("[012] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
  62. $host, $user, $db, $port, $socket);
  63. if (true !== ($ret = mysqli_set_charset($link, $new_charset)))
  64. printf("[013] Expecting boolean/true, got %s/%s\n", gettype($ret), $ret);
  65. if (!$res = mysqli_query($link, 'SELECT @@character_set_connection AS charset, @@collation_connection AS collation'))
  66. printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  67. $tmp = mysqli_fetch_assoc($res);
  68. mysqli_free_result($res);
  69. if ($new_charset !== $tmp['charset'])
  70. printf("[015] Character set not changed? Expecting %s, got %s\n", $new_charset, $tmp['charset']);
  71. if (!$res = mysqli_query($link, "SHOW CHARACTER SET"))
  72. printf("[016] Cannot get list of character sets\n");
  73. while ($tmp = mysqli_fetch_assoc($res)) {
  74. /* As of MySQL 8.0.28, `SHOW CHARACTER SET` contains utf8mb3, but that is not yet supported by mysqlnd */
  75. if ('ucs2' == $tmp['Charset'] || 'utf16' == $tmp['Charset'] || 'utf32' == $tmp['Charset'] || 'utf16le' == $tmp['Charset'] || 'utf8mb3' == $tmp['Charset'])
  76. continue;
  77. /* Uncomment to see where it hangs - var_dump($tmp); flush(); */
  78. if (!@mysqli_set_charset($link, $tmp['Charset'])) {
  79. printf("[017] Cannot set character set to '%s', [%d] %s\n", $tmp['Charset'],
  80. mysqli_errno($link), mysqli_error($link));
  81. continue;
  82. }
  83. /* Uncomment to see where it hangs - var_dump($tmp); flush(); */
  84. if (!mysqli_query($link, sprintf("SET NAMES %s", mysqli_real_escape_string($link, $tmp['Charset']))))
  85. printf("[018] Cannot run SET NAMES %s, [%d] %s\n", $tmp['Charset'], mysqli_errno($link), mysqli_error($link));
  86. }
  87. mysqli_free_result($res);
  88. // Make sure that set_charset throws an exception in exception mode
  89. mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  90. try {
  91. $link->set_charset('invalid');
  92. } catch (\mysqli_sql_exception $exception) {
  93. echo "Exception: " . $exception->getMessage() . "\n";
  94. }
  95. try {
  96. $link->set_charset('ucs2');
  97. } catch (\mysqli_sql_exception $exception) {
  98. echo "Exception: " . $exception->getMessage() . "\n";
  99. }
  100. mysqli_close($link);
  101. try {
  102. mysqli_set_charset($link, $new_charset);
  103. } catch (Error $exception) {
  104. echo $exception->getMessage() . "\n";
  105. }
  106. print "done!";
  107. ?>
  108. --CLEAN--
  109. <?php
  110. require_once("clean_table.inc");
  111. ?>
  112. --EXPECTF--
  113. Exception: %s
  114. Exception: Variable 'character_set_client' can't be set to the value of 'ucs2'
  115. mysqli object is already closed
  116. done!