1--TEST--
2Fetching results from tables of different charsets.
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once dirname(__DIR__) . "/test_setup/test_helpers.inc";
8mysqli_check_skip_test();
9?>
10--FILE--
11<?php
12/** This test requires MySQL Server 4.1+, which is now assumed  */
13require_once dirname(__DIR__) . "/test_setup/test_helpers.inc";
14
15$link = default_mysqli_connect();
16
17$result = mysqli_query($link, 'SHOW CHARACTER SET');
18$charsets = [];
19while ($row = mysqli_fetch_assoc($result)) {
20    $charsets[] = $row['Charset'];
21}
22mysqli_free_result($result);
23
24/* The server currently 17.07.2007 can't handle data sent in ucs2 */
25/* The server currently 16.08.2010 can't handle data sent in utf16 and utf32 */
26/* The server currently 02.09.2011 can't handle data sent in utf16le */
27/* As of MySQL 8.0.28, `SHOW CHARACTER SET` contains utf8mb3, but that is not yet supported by mysqlnd */
28const UNSUPPORTED_ENCODINGS = [
29    'ucs2',
30    'utf16',
31    'utf32',
32    'utf16le',
33    'utf8mb3',
34];
35
36foreach ($charsets as $charset) {
37    if (in_array($charset, UNSUPPORTED_ENCODINGS, true)) {
38        continue;
39    }
40
41    $sql = sprintf("CREATE TABLE test_mysqli_set_charset(id INT, label CHAR(1)) CHARACTER SET '%s' ", $charset);
42    mysqli_query($link, $sql);
43
44    mysqli_set_charset($link, $charset);
45
46    for ($i = 1; $i <= 3; $i++) {
47        mysqli_query(
48            $link,
49            sprintf(
50                "INSERT INTO test_mysqli_set_charset (id, label) VALUES (%d, '%s')",
51                $i,
52                mysqli_real_escape_string($link, chr(ord("a") + $i))
53            )
54        );
55    }
56
57    $result = mysqli_query($link, "SELECT id, label FROM test_mysqli_set_charset");
58    for ($i = 1; $i <= 3; $i++) {
59        $tmp = mysqli_fetch_assoc($result);
60        if ($tmp['id'] != $i)
61            printf("[012 + %s] Expecting %d, got %s, [%d] %s\n", $charset,
62                    $i, $tmp['id'],
63                    mysqli_errno($link), mysqli_error($link));
64
65        if ($tmp['label'] != chr(ord("a") + $i))
66            printf("[013 + %s] Expecting %d, got %s, [%d] %s\n", $charset,
67                chr(ord("a") + $i), $tmp['label'],
68                mysqli_errno($link), mysqli_error($link));
69    }
70    mysqli_free_result($result);
71    mysqli_query($link, "DROP TABLE test_mysqli_set_charset");
72}
73
74mysqli_close($link);
75
76print "done!";
77?>
78--CLEAN--
79<?php
80require_once dirname(__DIR__) . "/test_setup/test_helpers.inc";
81tear_down_table_on_default_connection('test_mysqli_set_charset');
82?>
83--EXPECT--
84done!
85