1--TEST--
2mysqli_get_charset()
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once 'skipifconnectfailure.inc';
8?>
9--FILE--
10<?php
11    require_once 'connect.inc';
12    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
13        printf("Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
14            $host, $user, $db, $port, $socket);
15        exit(1);
16    }
17
18    if (!$res = mysqli_query($link, 'SELECT @@character_set_connection AS charset, @@collation_connection AS collation'))
19        printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
20    $tmp = mysqli_fetch_assoc($res);
21    mysqli_free_result($res);
22    if (!($character_set_connection = $tmp['charset']) || !($collation_connection = $tmp['collation']))
23        printf("[008] Cannot determine current character set and collation\n");
24
25    if (!$res = mysqli_query($link, $sql = sprintf("SHOW CHARACTER SET LIKE '%s'", $character_set_connection)))
26        printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
27    if (!mysqli_fetch_assoc($res))
28        printf("[010] Cannot fetch Maxlen and/or Comment, test will fail: $sql\n");
29
30    if (!$res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $collation_connection)))
31        printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
32    $tmp = mysqli_fetch_assoc($res);
33    mysqli_free_result($res);
34    if (!($id = $tmp['Id']))
35        printf("[012] Cannot fetch Id/Number, test will fail\n");
36
37    if (!$res = mysqli_query($link, sprintf("SHOW VARIABLES LIKE 'character_sets_dir'")))
38        printf("[013] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
39    $tmp = mysqli_fetch_assoc($res);
40    mysqli_free_result($res);
41    if (!($character_sets_dir = $tmp['Value']))
42        printf("[014] Cannot fetch character_sets_dir, test will fail\n");
43
44    if (!is_object($charset = mysqli_get_charset($link)))
45        printf("[015] Expecting object/std_class, got %s/%s\n", gettype($charset), $charset);
46
47    if (!isset($charset->charset) ||
48        !is_string($charset->charset) ||
49        $character_set_connection !== $charset->charset)
50        printf("[016] Expecting string/%s, got %s/%s\n", $character_set_connection, gettype($charset->charset), $charset->charset);
51    if (!isset($charset->collation) ||
52        !is_string($charset->collation) ||
53        $collation_connection !== $charset->collation)
54        printf("[017] Expecting string/%s, got %s/%s\n", $collation_connection, gettype($charset->collation), $charset->collation);
55
56    if (!isset($charset->dir) ||
57        !is_string($charset->dir))
58        printf("[019] Expecting string - ideally %s*, got %s/%s\n", $character_sets_dir, gettype($charset->dir), $charset->dir);
59
60    if (!isset($charset->min_length) ||
61        !(is_int($charset->min_length)) ||
62        ($charset->min_length < 0) ||
63        ($charset->min_length > $charset->max_length))
64        printf("[020] Expecting int between 0 ... %d, got %s/%s\n", $charset->max_length,
65            gettype($charset->min_length), $charset->min_length);
66
67    if (!isset($charset->number) ||
68        !is_int($charset->number) ||
69        ($charset->number !== (int)$id))
70        printf("[021] Expecting int/%d, got %s/%s\n", $id, gettype($charset->number), $charset->number);
71
72    if (!isset($charset->state) ||
73        !is_int($charset->state))
74        printf("[022] Expecting int/any, got %s/%s\n", gettype($charset->state), $charset->state);
75
76    mysqli_close($link);
77
78    try {
79        mysqli_get_charset($link);
80    } catch (Error $exception) {
81        echo $exception->getMessage() . "\n";
82    }
83
84    print "done!";
85?>
86--EXPECT--
87mysqli object is already closed
88done!
89