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