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