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