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