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