1--TEST--
2Fetching results from tables of different charsets.
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once('skipifconnectfailure.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        /* As of MySQL 8.0.28, `SHOW CHARACTER SET` contains utf8mb3, but that is not yet supported by mysqlnd */
49        if ($charset['Charset'] == 'ucs2' || $charset['Charset'] == 'utf16' || $charset['Charset'] == 'utf32' || 'utf16le' == $charset['Charset'] || 'utf8mb3' == $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--EXPECT--
108done!
109