mysqli_set_charset.phpt 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 (version_compare(PHP_VERSION, '5.9.9', '>') == 1) {
  9. die('skip set character set not functional with PHP 6 (fomerly PHP 6 && unicode.semantics=On)');
  10. }
  11. if (!function_exists('mysqli_set_charset'))
  12. die("skip Function not available");
  13. require_once('connect.inc');
  14. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  15. die(sprintf("skip Cannot connect, [%d] %s", mysqli_connect_errno(), mysqli_connect_error()));
  16. if (!($res = mysqli_query($link, 'SELECT version() AS server_version')) ||
  17. !($tmp = mysqli_fetch_assoc($res))) {
  18. mysqli_close($link);
  19. die(sprintf("skip Cannot check server version, [%d] %s\n",
  20. mysqli_errno($link), mysqli_error($link)));
  21. }
  22. mysqli_free_result($res);
  23. $version = explode('.', $tmp['server_version']);
  24. if (empty($version)) {
  25. mysqli_close($link);
  26. die(sprintf("skip Cannot check server version, based on '%s'",
  27. $tmp['server_version']));
  28. }
  29. if ($version[0] <= 4 && $version[1] < 1) {
  30. mysqli_close($link);
  31. die(sprintf("skip Requires MySQL Server 4.1+\n"));
  32. }
  33. if ((($res = mysqli_query($link, 'SHOW CHARACTER SET LIKE "latin1"', MYSQLI_STORE_RESULT)) &&
  34. (mysqli_num_rows($res) == 1)) ||
  35. (($res = mysqli_query($link, 'SHOW CHARACTER SET LIKE "latin2"', MYSQLI_STORE_RESULT)) &&
  36. (mysqli_num_rows($res) == 1))
  37. ) {
  38. // ok, required latin1 or latin2 are available
  39. mysqli_close($link);
  40. } else {
  41. die(sprintf("skip Requires character set latin1 or latin2\n"));
  42. mysqli_close($link);
  43. }
  44. ?>
  45. --FILE--
  46. <?php
  47. require_once("connect.inc");
  48. $tmp = NULL;
  49. $link = NULL;
  50. if (!is_null($tmp = @mysqli_set_charset()))
  51. printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  52. if (!is_null($tmp = @mysqli_set_charset($link)))
  53. printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  54. if (!is_null($tmp = @mysqli_set_charset($link, $link)))
  55. printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  56. require('table.inc');
  57. if (!$res = mysqli_query($link, 'SELECT @@character_set_connection AS charset, @@collation_connection AS collation'))
  58. printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  59. $tmp = mysqli_fetch_assoc($res);
  60. mysqli_free_result($res);
  61. if (!$character_set_connection = $tmp['charset'])
  62. printf("[008] Cannot determine current character set and collation\n");
  63. $new_charset = ('latin1' == $character_set_connection) ? 'latin2' : 'latin1';
  64. if (!$res = mysqli_query($link, sprintf('SHOW CHARACTER SET LIKE "%s"', $new_charset), MYSQLI_STORE_RESULT))
  65. printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  66. if (mysqli_num_rows($res) == 0)
  67. printf("[010] Test will fail, because alternative test character set '%s' seems not supported\n", $new_charset);
  68. if (false !== ($ret = mysqli_set_charset($link, "this is not a valid character set")))
  69. printf("[011] Expecting boolean/false because of invalid character set, got %s/%s\n", gettype($ret), $ret);
  70. mysqli_close($link);
  71. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  72. printf("[012] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
  73. $host, $user, $db, $port, $socket);
  74. if (true !== ($ret = mysqli_set_charset($link, $new_charset)))
  75. printf("[013] Expecting boolean/true, got %s/%s\n", gettype($ret), $ret);
  76. if (!$res = mysqli_query($link, 'SELECT @@character_set_connection AS charset, @@collation_connection AS collation'))
  77. printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  78. $tmp = mysqli_fetch_assoc($res);
  79. mysqli_free_result($res);
  80. if ($new_charset !== $tmp['charset'])
  81. printf("[015] Character set not changed? Expecting %s, got %s\n", $new_charset, $tmp['charset']);
  82. if (!$res = mysqli_query($link, "SHOW CHARACTER SET"))
  83. printf("[016] Cannot get list of character sets\n");
  84. while ($tmp = mysqli_fetch_assoc($res)) {
  85. if ('ucs2' == $tmp['Charset'] || 'utf16' == $tmp['Charset'] || 'utf32' == $tmp['Charset'] || 'utf16le' == $tmp['Charset'])
  86. continue;
  87. /* Uncomment to see where it hangs - var_dump($tmp); flush(); */
  88. if (!@mysqli_set_charset($link, $tmp['Charset'])) {
  89. printf("[017] Cannot set character set to '%s', [%d] %s\n", $tmp['Charset'],
  90. mysqli_errno($link), mysqli_error($link));
  91. continue;
  92. }
  93. /* Uncomment to see where it hangs - var_dump($tmp); flush(); */
  94. if (!mysqli_query($link, sprintf("SET NAMES %s", mysqli_real_escape_string($link, $tmp['Charset']))))
  95. printf("[018] Cannot run SET NAMES %s, [%d] %s\n", $tmp['Charset'], mysqli_errno($link), mysqli_error($link));
  96. }
  97. mysqli_free_result($res);
  98. mysqli_close($link);
  99. if (NULL !== ($tmp = mysqli_set_charset($link, $new_charset)))
  100. printf("[016] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  101. print "done!";
  102. ?>
  103. --CLEAN--
  104. <?php
  105. require_once("clean_table.inc");
  106. ?>
  107. --EXPECTF--
  108. Warning: mysqli_set_charset(): Couldn't fetch mysqli in %s on line %d
  109. done!