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, 'SELECT version() AS server_version')) ||
12        !($tmp = mysqli_fetch_assoc($res))) {
13    mysqli_close($link);
14    die(sprintf("skip Cannot check server version, [%d] %s\n",
15    mysqli_errno($link), mysqli_error($link)));
16}
17mysqli_free_result($res);
18$version = explode('.', $tmp['server_version']);
19if (empty($version)) {
20    mysqli_close($link);
21    die(sprintf("skip Cannot check server version, based on '%s'",
22        $tmp['server_version']));
23}
24
25if ($version[0] <= 4 && $version[1] < 1) {
26    mysqli_close($link);
27    die(sprintf("skip Requires MySQL Server 4.1+\n"));
28}
29
30if ((($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 'table.inc';
45
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
53    $new_charset = ('latin1' == $character_set_connection) ? 'latin2' : 'latin1';
54    if (!$res = mysqli_query($link, sprintf('SHOW CHARACTER SET LIKE "%s"', $new_charset), MYSQLI_STORE_RESULT))
55        printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
56
57    if (mysqli_num_rows($res) == 0)
58        printf("[010] Test will fail, because alternative test character set '%s' seems not supported\n", $new_charset);
59
60    if (false !== ($ret = mysqli_set_charset($link, "this is not a valid character set")))
61        printf("[011] Expecting boolean/false because of invalid character set, got %s/%s\n", gettype($ret), $ret);
62
63    mysqli_close($link);
64    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
65        printf("[012] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
66            $host, $user, $db, $port, $socket);
67
68    if (true !== ($ret = mysqli_set_charset($link, $new_charset)))
69        printf("[013] Expecting boolean/true, got %s/%s\n", gettype($ret), $ret);
70
71    if (!$res = mysqli_query($link, 'SELECT @@character_set_connection AS charset, @@collation_connection AS collation'))
72        printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
73    $tmp = mysqli_fetch_assoc($res);
74    mysqli_free_result($res);
75    if ($new_charset !== $tmp['charset'])
76        printf("[015] Character set not changed? Expecting %s, got %s\n", $new_charset, $tmp['charset']);
77
78    if (!$res = mysqli_query($link, "SHOW CHARACTER SET"))
79        printf("[016] Cannot get list of character sets\n");
80
81    while ($tmp = mysqli_fetch_assoc($res)) {
82        /* As of MySQL 8.0.28, `SHOW CHARACTER SET` contains utf8mb3, but that is not yet supported by mysqlnd */
83        if ('ucs2' == $tmp['Charset'] || 'utf16' == $tmp['Charset'] || 'utf32' == $tmp['Charset'] || 'utf16le' == $tmp['Charset'] || 'utf8mb3' == $tmp['Charset'])
84            continue;
85
86        /* Uncomment to see where it hangs - var_dump($tmp); flush(); */
87        if (!@mysqli_set_charset($link, $tmp['Charset'])) {
88            printf("[017] Cannot set character set to '%s', [%d] %s\n", $tmp['Charset'],
89                mysqli_errno($link), mysqli_error($link));
90            continue;
91        }
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
99    // Make sure that set_charset throws an exception in exception mode
100    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
101    try {
102        $link->set_charset('invalid');
103    } catch (\mysqli_sql_exception $exception) {
104        echo "Exception: " . $exception->getMessage() . "\n";
105    }
106
107    try {
108        $link->set_charset('ucs2');
109    } catch (\mysqli_sql_exception $exception) {
110        echo "Exception: " . $exception->getMessage() . "\n";
111    }
112
113    mysqli_close($link);
114
115    try {
116        mysqli_set_charset($link, $new_charset);
117    } catch (Error $exception) {
118        echo $exception->getMessage() . "\n";
119    }
120
121    print "done!";
122?>
123--CLEAN--
124<?php
125require_once 'clean_table.inc';
126?>
127--EXPECTF--
128Exception: %s
129Exception: Variable 'character_set_client' can't be set to the value of 'ucs2'
130mysqli object is already closed
131done!
132