1--TEST--
2Fetching results from tables of different charsets.
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7
8if (!function_exists('mysqli_set_charset')) {
9    die('skip mysqli_set_charset() not available');
10}
11?>
12--FILE--
13<?php
14    require_once("connect.inc");
15
16    $tmp	= NULL;
17    $link	= NULL;
18    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
19        printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
20            $host, $user, $db, $port, $socket);
21
22    if (!$res = mysqli_query($link, 'SELECT version() AS server_version'))
23        printf("[002] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
24    $tmp = mysqli_fetch_assoc($res);
25    mysqli_free_result($res);
26    $version = explode('.', $tmp['server_version']);
27    if (empty($version))
28        printf("[003] Cannot determine server version, need MySQL Server 4.1+ for the test!\n");
29
30    if ($version[0] <= 4 && $version[1] < 1)
31        printf("[004] Need MySQL Server 4.1+ for the test!\n");
32
33    if (!$res = mysqli_query($link, "SHOW CHARACTER SET"))
34        printf("[005] Cannot get list of available character sets, [%d] %s\n",
35            mysqli_errno($link), mysqli_error($link));
36
37    $charsets = array();
38    while ($row = mysqli_fetch_assoc($res))
39        $charsets[] = $row;
40    mysqli_free_result($res);
41
42    foreach ($charsets as $charset) {
43        $k = $charset['Charset'];
44        /* The server currently 17.07.2007 can't handle data sent in ucs2 */
45        /* The server currently 16.08.2010 can't handle data sent in utf16 and utf32 */
46        /* The server currently 02.09.2011 can't handle data sent in utf16le */
47        /* As of MySQL 8.0.28, `SHOW CHARACTER SET` contains utf8mb3, but that is not yet supported by mysqlnd */
48        if ($charset['Charset'] == 'ucs2' || $charset['Charset'] == 'utf16' || $charset['Charset'] == 'utf32' || 'utf16le' == $charset['Charset'] || 'utf8mb3' == $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