1--TEST--
2mysqli_set_charset()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
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    $tmp    = NULL;
52    $link   = NULL;
53
54    if (!is_null($tmp = @mysqli_set_charset()))
55        printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
56
57    if (!is_null($tmp = @mysqli_set_charset($link)))
58        printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
59
60    if (!is_null($tmp = @mysqli_set_charset($link, $link)))
61        printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
62
63    require('table.inc');
64
65    if (!$res = mysqli_query($link, 'SELECT @@character_set_connection AS charset, @@collation_connection AS collation'))
66        printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
67    $tmp = mysqli_fetch_assoc($res);
68    mysqli_free_result($res);
69    if (!$character_set_connection = $tmp['charset'])
70        printf("[008] Cannot determine current character set and collation\n");
71
72    $new_charset = ('latin1' == $character_set_connection) ? 'latin2' : 'latin1';
73    if (!$res = mysqli_query($link, sprintf('SHOW CHARACTER SET LIKE "%s"', $new_charset), MYSQLI_STORE_RESULT))
74        printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
75
76    if (mysqli_num_rows($res) == 0)
77        printf("[010] Test will fail, because alternative test character set '%s' seems not supported\n", $new_charset);
78
79    if (false !== ($ret = mysqli_set_charset($link, "this is not a valid character set")))
80        printf("[011] Expecting boolean/false because of invalid character set, got %s/%s\n", gettype($ret), $ret);
81
82    mysqli_close($link);
83    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
84        printf("[012] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
85            $host, $user, $db, $port, $socket);
86
87    if (true !== ($ret = mysqli_set_charset($link, $new_charset)))
88        printf("[013] Expecting boolean/true, got %s/%s\n", gettype($ret), $ret);
89
90    if (!$res = mysqli_query($link, 'SELECT @@character_set_connection AS charset, @@collation_connection AS collation'))
91        printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
92    $tmp = mysqli_fetch_assoc($res);
93    mysqli_free_result($res);
94    if ($new_charset !== $tmp['charset'])
95        printf("[015] Character set not changed? Expecting %s, got %s\n", $new_charset, $tmp['charset']);
96
97    if (!$res = mysqli_query($link, "SHOW CHARACTER SET"))
98        printf("[016] Cannot get list of character sets\n");
99
100    while ($tmp = mysqli_fetch_assoc($res)) {
101        if ('ucs2' == $tmp['Charset'] || 'utf16' == $tmp['Charset'] || 'utf32' == $tmp['Charset'] || 'utf16le' == $tmp['Charset'])
102            continue;
103
104        /* Uncomment to see where it hangs - var_dump($tmp); flush(); */
105        if (!@mysqli_set_charset($link, $tmp['Charset'])) {
106            printf("[017] Cannot set character set to '%s', [%d] %s\n", $tmp['Charset'],
107                mysqli_errno($link), mysqli_error($link));
108            continue;
109        }
110
111        /* Uncomment to see where it hangs - var_dump($tmp); flush(); */
112        if (!mysqli_query($link, sprintf("SET NAMES %s", mysqli_real_escape_string($link, $tmp['Charset']))))
113            printf("[018] Cannot run SET NAMES %s, [%d] %s\n", $tmp['Charset'], mysqli_errno($link), mysqli_error($link));
114    }
115    mysqli_free_result($res);
116
117    mysqli_close($link);
118
119    if (false !== ($tmp = mysqli_set_charset($link, $new_charset)))
120        printf("[019] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
121
122    print "done!";
123?>
124--CLEAN--
125<?php
126    require_once("clean_table.inc");
127?>
128--EXPECTF--
129Warning: mysqli_set_charset(): Couldn't fetch mysqli in %s on line %d
130done!
131