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