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    if (!$IS_MYSQLND) {
95        print "Unknown character set\n";
96    }
97} catch (mysqli_sql_exception $e) {
98    echo $e->getMessage() . "\n";
99}
100
101// invalid options do not generate errors
102mysqli_options($link, -1, "Invalid option");
103
104print "done!";
105?>
106--EXPECTF--
107%s(25) "MYSQLI_READ_DEFAULT_GROUP"
108bool(true)
109%s(24) "MYSQLI_READ_DEFAULT_FILE"
110bool(true)
111%s(26) "MYSQLI_OPT_CONNECT_TIMEOUT"
112bool(true)
113%s(23) "MYSQLI_OPT_LOCAL_INFILE"
114bool(true)
115
116Warning: Array to string conversion in %s on line %d
117%s(19) "MYSQLI_INIT_COMMAND"
118bool(true)
119%s(25) "MYSQLI_READ_DEFAULT_GROUP"
120bool(true)
121%s(24) "MYSQLI_READ_DEFAULT_FILE"
122bool(true)
123%s(26) "MYSQLI_OPT_CONNECT_TIMEOUT"
124bool(true)
125%s(23) "MYSQLI_OPT_LOCAL_INFILE"
126bool(true)
127%s(19) "MYSQLI_INIT_COMMAND"
128bool(true)
129%s(17) "MYSQLI_CLIENT_SSL"
130bool(false)
131Link closed
132mysqli object is already closed
133Unknown character set
134done!
135