xref: /PHP-8.1/ext/mysqli/tests/065.phpt (revision b5a14e6c)
1--TEST--
2set character set
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    if (!$mysql = new my_mysqli($host, $user, $passwd, $db, $port, $socket))
18        printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
19
20    if (!mysqli_query($mysql, "SET sql_mode=''"))
21        printf("[002] Cannot set SQL-Mode, [%d] %s\n", mysqli_errno($mysql), mysqli_error($mysql));
22
23    $esc_str = chr(0xbf) . chr(0x5c);
24    $len = $charset = array();
25    $tmp = null;
26
27    if ($mysql->set_charset("latin1")) {
28        /* 5C should be escaped */
29        if (3 !== ($tmp = strlen($mysql->real_escape_string($esc_str))))
30            printf("[003] Expecting 3/int got %s/%s\n", gettype($tmp), $tmp);
31
32        if ('latin1' !== ($tmp = $mysql->character_set_name()))
33            printf("[004] Expecting latin1/string got %s/%s\n", gettype($tmp), $tmp);
34    }
35
36    if ($res = $mysql->query("SHOW CHARACTER SET LIKE 'gbk'")) {
37        $res->free_result();
38        if ($mysql->set_charset("gbk")) {
39            /* nothing should be escaped, it's a valid gbk character */
40
41            if (2 !== ($tmp = strlen($mysql->real_escape_string($esc_str))))
42                    printf("[005] Expecting 2/int got %s/%s\n", gettype($tmp), $tmp);
43
44            if ('gbk' !== ($tmp = $mysql->character_set_name()))
45                    printf("[005] Expecting gbk/string got %s/%s\n", gettype($tmp), $tmp);
46        }
47    }
48    $mysql->close();
49
50    print "done!";
51?>
52--EXPECT--
53done!
54