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