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