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