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