1--TEST--
2mysqli_get_charset()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7if (!function_exists('mysqli_get_charset'))
8    die("skip: function not available");
9?>
10--FILE--
11<?php
12    require_once("connect.inc");
13
14    require('table.inc');
15
16    if (!$res = mysqli_query($link, 'SELECT version() AS server_version'))
17        printf("[004] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
18    $tmp = mysqli_fetch_assoc($res);
19    mysqli_free_result($res);
20    $version = explode('.', $tmp['server_version']);
21    if (empty($version))
22        printf("[005] Cannot determine server version, need MySQL Server 4.1+ for the test!\n");
23
24    if ($version[0] <= 4 && $version[1] < 1)
25        printf("[006] Need MySQL Server 4.1+ for the test!\n");
26
27    if (!$res = mysqli_query($link, 'SELECT @@character_set_connection AS charset, @@collation_connection AS collation'))
28        printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
29    $tmp = mysqli_fetch_assoc($res);
30    mysqli_free_result($res);
31    if (!($character_set_connection = $tmp['charset']) || !($collation_connection = $tmp['collation']))
32        printf("[008] Cannot determine current character set and collation\n");
33
34    if (!$res = mysqli_query($link, $sql = sprintf("SHOW CHARACTER SET LIKE '%s'", $character_set_connection)))
35        printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
36    $tmp = mysqli_fetch_assoc($res);
37    if (empty($tmp))
38        printf("[010] Cannot fetch Maxlen and/or Comment, test will fail: $sql\n");
39
40    $maxlen = (isset($tmp['Maxlen'])) ? $tmp['Maxlen'] : '';
41    $comment = (isset($tmp['Description'])) ? $tmp['Description'] : '';
42
43    if (!$res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $collation_connection)))
44        printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
45    $tmp = mysqli_fetch_assoc($res);
46    mysqli_free_result($res);
47    if (!($id = $tmp['Id']))
48        printf("[012] Cannot fetch Id/Number, test will fail\n");
49
50    if (!$res = mysqli_query($link, sprintf("SHOW VARIABLES LIKE 'character_sets_dir'")))
51        printf("[013] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
52    $tmp = mysqli_fetch_assoc($res);
53    mysqli_free_result($res);
54    if (!($character_sets_dir = $tmp['Value']))
55        printf("[014] Cannot fetch character_sets_dir, test will fail\n");
56
57    if (!is_object($charset = mysqli_get_charset($link)))
58        printf("[015] Expecting object/std_class, got %s/%s\n", gettype($charset), $charset);
59
60    if (!isset($charset->charset) ||
61        !in_array(gettype($charset->charset), array("string", "unicode")) ||
62        ($character_set_connection !== $charset->charset))
63        printf("[016] Expecting string/%s, got %s/%s\n", $character_set_connection, gettype($charset->charset), $charset->charset);
64    if (!isset($charset->collation) ||
65        !in_array(gettype($charset->collation), array("string", "unicode")) ||
66        ($collation_connection !== $charset->collation))
67        printf("[017] Expecting string/%s, got %s/%s\n", $collation_connection, gettype($charset->collation), $charset->collation);
68
69    if (!isset($charset->dir) ||
70        !is_string($charset->dir))
71        printf("[019] Expecting string - ideally %s*, got %s/%s\n", $character_sets_dir, gettype($charset->dir), $charset->dir);
72
73    if (!isset($charset->min_length) ||
74        !(is_int($charset->min_length)) ||
75        ($charset->min_length < 0) ||
76        ($charset->min_length > $charset->max_length))
77        printf("[020] Expecting int between 0 ... %d, got %s/%s\n", $charset->max_length,
78            gettype($charset->min_length), $charset->min_length);
79
80    if (!isset($charset->number) ||
81        !is_int($charset->number) ||
82        ($charset->number !== (int)$id))
83        printf("[021] Expecting int/%d, got %s/%s\n", $id, gettype($charset->number), $charset->number);
84
85    if (!isset($charset->state) ||
86        !is_int($charset->state))
87        printf("[022] Expecting int/any, got %s/%s\n", gettype($charset->state), $charset->state);
88
89    mysqli_close($link);
90
91    try {
92        mysqli_get_charset($link);
93    } catch (Error $exception) {
94        echo $exception->getMessage() . "\n";
95    }
96
97    print "done!";
98?>
99--CLEAN--
100<?php
101    require_once("clean_table.inc");
102?>
103--EXPECT--
104mysqli object is already closed
105done!
106