1--TEST--
2mysqli_options()
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once 'skipifconnectfailure.inc';
8
9?>
10--FILE--
11<?php
12require_once "connect.inc";
13
14$link = mysqli_init();
15
16/* set it twice, checking if memory for the previous one is correctly freed */
17mysqli_options($link, MYSQLI_SET_CHARSET_NAME, "utf8");
18mysqli_options($link, MYSQLI_SET_CHARSET_NAME, "latin1");
19
20// print "run_tests.php don't fool me with your 'ungreedy' expression '.+?'!\n";
21var_dump("MYSQLI_READ_DEFAULT_GROUP", mysqli_options($link, MYSQLI_READ_DEFAULT_GROUP, 'extra_my.cnf'));
22var_dump("MYSQLI_READ_DEFAULT_FILE", mysqli_options($link, MYSQLI_READ_DEFAULT_FILE, 'extra_my.cnf'));
23var_dump("MYSQLI_OPT_CONNECT_TIMEOUT", mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 10));
24var_dump("MYSQLI_OPT_LOCAL_INFILE", mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, 1));
25var_dump("MYSQLI_INIT_COMMAND", mysqli_options($link, MYSQLI_INIT_COMMAND, array('SET AUTOCOMMIT=0', 'SET AUTOCOMMIT=1')));
26
27if (!$link2 = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
28    printf("[006] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
29        $host, $user, $db, $port, $socket);
30}
31
32if (!$res = mysqli_query($link2, 'SELECT version() AS server_version')) {
33    printf("[007] [%d] %s\n", mysqli_errno($link2), mysqli_error($link2));
34}
35$tmp = mysqli_fetch_assoc($res);
36mysqli_free_result($res);
37$version = explode('.', $tmp['server_version']);
38if (empty($version)) {
39    printf("[008] Cannot determine server version, need MySQL Server 4.1+ for the test!\n");
40}
41
42if ($version[0] <= 4 && $version[1] < 1) {
43    printf("[009] Need MySQL Server 4.1+ for the test!\n");
44}
45
46if (!$res = mysqli_query($link2, "SHOW CHARACTER SET")) {
47    printf("[010] Cannot get list of available character sets, [%d] %s\n",
48        mysqli_errno($link2), mysqli_error($link2));
49}
50
51$charsets = array();
52while ($row = mysqli_fetch_assoc($res)) {
53    $charsets[] = $row;
54}
55mysqli_free_result($res);
56mysqli_close($link2);
57
58foreach ($charsets as $charset) {
59    /* The server currently 17.07.2007 can't handle data sent in ucs2 */
60    /* The server currently 16.08.2010 can't handle data sent in utf16 and utf32 */
61    /* As of MySQL 8.0.28, `SHOW CHARACTER SET` contains utf8mb3, but that is not yet supported by mysqlnd */
62    if ($charset['Charset'] == 'ucs2' || $charset['Charset'] == 'utf16' || $charset['Charset'] == 'utf32' || $charset['Charset'] == 'utf8mb3') {
63        continue;
64    }
65    if (true !== mysqli_options($link, MYSQLI_SET_CHARSET_NAME, $charset['Charset'])) {
66        printf("[009] Setting charset name '%s' has failed\n", $charset['Charset']);
67    }
68}
69
70var_dump("MYSQLI_READ_DEFAULT_GROUP", mysqli_options($link, MYSQLI_READ_DEFAULT_GROUP, 'extra_my.cnf'));
71var_dump("MYSQLI_READ_DEFAULT_FILE", mysqli_options($link, MYSQLI_READ_DEFAULT_FILE, 'extra_my.cnf'));
72var_dump("MYSQLI_OPT_CONNECT_TIMEOUT", mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 10));
73var_dump("MYSQLI_OPT_LOCAL_INFILE", mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, 1));
74var_dump("MYSQLI_INIT_COMMAND", mysqli_options($link, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT=0'));
75
76/* mysqli_real_connect() */
77var_dump("MYSQLI_CLIENT_SSL", mysqli_options($link, MYSQLI_CLIENT_SSL, 'not a mysqli_option'));
78
79mysqli_close($link);
80
81echo "Link closed\n";
82try {
83    mysqli_options($link, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT=1');
84} catch (Error $exception) {
85    echo $exception->getMessage() . "\n";
86}
87
88mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
89$link = mysqli_init();
90
91// test for error reporting - only mysqlnd reports errors
92try {
93    mysqli_options($link, MYSQLI_SET_CHARSET_NAME, "foobar");
94} catch (mysqli_sql_exception $e) {
95    echo $e->getMessage() . "\n";
96}
97
98// invalid options do not generate errors
99mysqli_options($link, -1, "Invalid option");
100
101print "done!";
102?>
103--EXPECTF--
104%s(25) "MYSQLI_READ_DEFAULT_GROUP"
105bool(true)
106%s(24) "MYSQLI_READ_DEFAULT_FILE"
107bool(true)
108%s(26) "MYSQLI_OPT_CONNECT_TIMEOUT"
109bool(true)
110%s(23) "MYSQLI_OPT_LOCAL_INFILE"
111bool(true)
112
113Warning: Array to string conversion in %s on line %d
114%s(19) "MYSQLI_INIT_COMMAND"
115bool(true)
116%s(25) "MYSQLI_READ_DEFAULT_GROUP"
117bool(true)
118%s(24) "MYSQLI_READ_DEFAULT_FILE"
119bool(true)
120%s(26) "MYSQLI_OPT_CONNECT_TIMEOUT"
121bool(true)
122%s(23) "MYSQLI_OPT_LOCAL_INFILE"
123bool(true)
124%s(19) "MYSQLI_INIT_COMMAND"
125bool(true)
126%s(17) "MYSQLI_CLIENT_SSL"
127bool(false)
128Link closed
129mysqli object is already closed
130Unknown character set
131done!
132