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