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