1--TEST--
2Fetching results from tables of different charsets.
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7require_once('skipifunicode.inc');
8require_once('skipifemb.inc');
9
10if (!function_exists('mysqli_set_charset')) {
11	die('skip mysqli_set_charset() not available');
12}
13if (version_compare(PHP_VERSION, '5.9.9', '>') == 1) {
14	die('skip set character set not functional with PHP 6 (fomerly PHP 6 && unicode.semantics=On)');
15}
16?>
17--FILE--
18<?php
19	require_once("connect.inc");
20
21	$tmp	= NULL;
22	$link	= NULL;
23	if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
24		printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
25			$host, $user, $db, $port, $socket);
26
27	if (!$res = mysqli_query($link, 'SELECT version() AS server_version'))
28		printf("[002] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
29	$tmp = mysqli_fetch_assoc($res);
30	mysqli_free_result($res);
31	$version = explode('.', $tmp['server_version']);
32	if (empty($version))
33		printf("[003] Cannot determine server version, need MySQL Server 4.1+ for the test!\n");
34
35	if ($version[0] <= 4 && $version[1] < 1)
36		printf("[004] Need MySQL Server 4.1+ for the test!\n");
37
38	if (!$res = mysqli_query($link, "SHOW CHARACTER SET"))
39		printf("[005] Cannot get list of available character sets, [%d] %s\n",
40			mysqli_errno($link), mysqli_error($link));
41
42	$charsets = array();
43	while ($row = mysqli_fetch_assoc($res))
44		$charsets[] = $row;
45	mysqli_free_result($res);
46
47	foreach ($charsets as $charset) {
48		$k = $charset['Charset'];
49		/* The server currently 17.07.2007 can't handle data sent in ucs2 */
50		/* The server currently 16.08.2010 can't handle data sent in utf16 and utf32 */
51		/* The server currently 02.09.2011 can't handle data sent in utf16le */
52		if ($charset['Charset'] == 'ucs2' || $charset['Charset'] == 'utf16' || $charset['Charset'] == 'utf32' || 'utf16le' == $charset['Charset']) {
53			continue;
54		}
55
56		if (!mysqli_query($link, "DROP TABLE IF EXISTS test"))
57			printf("[006 + %s] [%d] %s\n", $k, mysqli_errno($link), mysqli_error($link));
58
59		$sql = sprintf("CREATE TABLE test(id INT, label CHAR(1)) CHARACTER SET '%s' ", $charset['Charset']);
60		if (!mysqli_query($link, $sql)) {
61			printf("[007 + %s] %s [%d] %s\n", $k, $sql, mysqli_errno($link), mysqli_error($link));
62			continue;
63		}
64
65		if (!mysqli_set_charset($link, $charset['Charset'])) {
66			printf("[008 + %s] [%d] %s\n", $k, mysqli_errno($link), mysqli_error($link));
67			continue;
68		}
69
70		for ($i = 1; $i <= 3; $i++) {
71			if (!mysqli_query($link, sprintf("INSERT INTO test (id, label) VALUES (%d, '%s')",
72								$i, mysqli_real_escape_string($link, chr(ord("a") + $i)))))
73			{
74				var_dump($charset['Charset']);
75				printf("[009 + %s] [%d] %s\n", $k, mysqli_errno($link), mysqli_error($link));
76				continue;
77			}
78		}
79
80		if (!$res = mysqli_query($link, "SELECT id, label FROM test"))
81			printf("[010 + %s] [%d] %s\n", $k, mysqli_errno($link), mysqli_error($link));
82
83		for ($i = 1; $i <= 3; $i++) {
84
85			if (!$tmp = mysqli_fetch_assoc($res))
86				printf("[011 + %s] [%d] %s\n", $k, mysqli_errno($link), mysqli_error($link));
87
88			if ($tmp['id'] != $i)
89				printf("[012 + %s] Expecting %d, got %s, [%d] %s\n", $k,
90						$i, $tmp['id'],
91						mysqli_errno($link), mysqli_error($link));
92
93			if ($tmp['label'] != chr(ord("a") + $i))
94				printf("[013 + %s] Expecting %d, got %s, [%d] %s\n", $k,
95					chr(ord("a") + $i), $tmp['label'],
96					mysqli_errno($link), mysqli_error($link));
97
98		}
99		mysqli_free_result($res);
100	}
101
102	mysqli_close($link);
103
104	print "done!";
105?>
106--CLEAN--
107<?php
108	require_once("clean_table.inc");
109?>
110--EXPECTF--
111done!
112