mysqli_set_charset.phpt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. --TEST--
  2. mysqli_set_charset()
  3. --SKIPIF--
  4. <?php
  5. require_once('skipif.inc');
  6. require_once('skipifemb.inc');
  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. $tmp = NULL;
  46. $link = NULL;
  47. if (!is_null($tmp = @mysqli_set_charset()))
  48. printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  49. if (!is_null($tmp = @mysqli_set_charset($link)))
  50. printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  51. if (!is_null($tmp = @mysqli_set_charset($link, $link)))
  52. printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  53. require('table.inc');
  54. if (!$res = mysqli_query($link, 'SELECT @@character_set_connection AS charset, @@collation_connection AS collation'))
  55. printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  56. $tmp = mysqli_fetch_assoc($res);
  57. mysqli_free_result($res);
  58. if (!$character_set_connection = $tmp['charset'])
  59. printf("[008] Cannot determine current character set and collation\n");
  60. $new_charset = ('latin1' == $character_set_connection) ? 'latin2' : 'latin1';
  61. if (!$res = mysqli_query($link, sprintf('SHOW CHARACTER SET LIKE "%s"', $new_charset), MYSQLI_STORE_RESULT))
  62. printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  63. if (mysqli_num_rows($res) == 0)
  64. printf("[010] Test will fail, because alternative test character set '%s' seems not supported\n", $new_charset);
  65. if (false !== ($ret = mysqli_set_charset($link, "this is not a valid character set")))
  66. printf("[011] Expecting boolean/false because of invalid character set, got %s/%s\n", gettype($ret), $ret);
  67. mysqli_close($link);
  68. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  69. printf("[012] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
  70. $host, $user, $db, $port, $socket);
  71. if (true !== ($ret = mysqli_set_charset($link, $new_charset)))
  72. printf("[013] Expecting boolean/true, got %s/%s\n", gettype($ret), $ret);
  73. if (!$res = mysqli_query($link, 'SELECT @@character_set_connection AS charset, @@collation_connection AS collation'))
  74. printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  75. $tmp = mysqli_fetch_assoc($res);
  76. mysqli_free_result($res);
  77. if ($new_charset !== $tmp['charset'])
  78. printf("[015] Character set not changed? Expecting %s, got %s\n", $new_charset, $tmp['charset']);
  79. if (!$res = mysqli_query($link, "SHOW CHARACTER SET"))
  80. printf("[016] Cannot get list of character sets\n");
  81. while ($tmp = mysqli_fetch_assoc($res)) {
  82. if ('ucs2' == $tmp['Charset'] || 'utf16' == $tmp['Charset'] || 'utf32' == $tmp['Charset'] || 'utf16le' == $tmp['Charset'])
  83. continue;
  84. /* Uncomment to see where it hangs - var_dump($tmp); flush(); */
  85. if (!@mysqli_set_charset($link, $tmp['Charset'])) {
  86. printf("[017] Cannot set character set to '%s', [%d] %s\n", $tmp['Charset'],
  87. mysqli_errno($link), mysqli_error($link));
  88. continue;
  89. }
  90. /* Uncomment to see where it hangs - var_dump($tmp); flush(); */
  91. if (!mysqli_query($link, sprintf("SET NAMES %s", mysqli_real_escape_string($link, $tmp['Charset']))))
  92. printf("[018] Cannot run SET NAMES %s, [%d] %s\n", $tmp['Charset'], mysqli_errno($link), mysqli_error($link));
  93. }
  94. mysqli_free_result($res);
  95. mysqli_close($link);
  96. if (false !== ($tmp = mysqli_set_charset($link, $new_charset)))
  97. printf("[019] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
  98. print "done!";
  99. ?>
  100. --CLEAN--
  101. <?php
  102. require_once("clean_table.inc");
  103. ?>
  104. --EXPECTF--
  105. Warning: mysqli_set_charset(): Couldn't fetch mysqli in %s on line %d
  106. done!