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