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_DEFAUTL_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
62	if (!$link2 = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
63		printf("[006] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
64			$host, $user, $db, $port, $socket);
65
66	if (!$res = mysqli_query($link2, 'SELECT version() AS server_version'))
67		printf("[007] [%d] %s\n", mysqli_errno($link2), mysqli_error($link2));
68	$tmp = mysqli_fetch_assoc($res);
69	mysqli_free_result($res);
70	$version = explode('.', $tmp['server_version']);
71	if (empty($version))
72		printf("[008] Cannot determine server version, need MySQL Server 4.1+ for the test!\n");
73
74	if ($version[0] <= 4 && $version[1] < 1)
75		printf("[009] Need MySQL Server 4.1+ for the test!\n");
76
77	if (!$res = mysqli_query($link2, "SHOW CHARACTER SET"))
78		printf("[010] Cannot get list of available character sets, [%d] %s\n",
79			mysqli_errno($link2), mysqli_error($link2));
80
81	$charsets = array();
82	while ($row = mysqli_fetch_assoc($res))
83		$charsets[] = $row;
84	mysqli_free_result($res);
85	mysqli_close($link2);
86
87	foreach ($charsets as $charset) {
88		$k = $charset['Charset'];
89		/* The server currently 17.07.2007 can't handle data sent in ucs2 */
90		/* The server currently 16.08.2010 can't handle data sent in utf16 and utf32 */
91		if ($charset['Charset'] == 'ucs2' || $charset['Charset'] == 'utf16' || $charset['Charset'] == 'utf32') {
92			continue;
93		}
94		if (true !== mysqli_options($link, MYSQLI_SET_CHARSET_NAME, $charset['Charset'])) {
95			printf("[009] Setting charset name '%s' has failed\n", $charset['Charset']);
96		}
97	}
98
99	var_dump("MYSQLI_READ_DEFAULT_GROUP",	mysqli_options($link, MYSQLI_READ_DEFAULT_GROUP, 'extra_my.cnf'));
100	var_dump("MYSQLI_READ_DEFAULT_FILE",	mysqli_options($link, MYSQLI_READ_DEFAULT_FILE, 'extra_my.cnf'));
101	var_dump("MYSQLI_OPT_CONNECT_TIMEOUT",	mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 10));
102	var_dump("MYSQLI_OPT_LOCAL_INFILE",		mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, 1));
103	var_dump("MYSQLI_INIT_COMMAND",			mysqli_options($link, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT=0'));
104
105	/* mysqli_real_connect() */
106	var_dump("MYSQLI_CLIENT_SSL",			mysqli_options($link, MYSQLI_CLIENT_SSL, 'not a mysqli_option'));
107
108	mysqli_close($link);
109
110	echo "Link closed";
111	var_dump("MYSQLI_INIT_COMMAND", mysqli_options($link, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT=1'));
112	print "done!";
113?>
114--EXPECTF--
115%s(25) "MYSQLI_READ_DEFAULT_GROUP"
116bool(true)
117%s(24) "MYSQLI_READ_DEFAULT_FILE"
118bool(true)
119%s(26) "MYSQLI_OPT_CONNECT_TIMEOUT"
120bool(true)
121%s(23) "MYSQLI_OPT_LOCAL_INFILE"
122bool(true)
123
124Notice: Array to string conversion in %s on line %d
125%s(19) "MYSQLI_INIT_COMMAND"
126bool(true)
127%s(25) "MYSQLI_READ_DEFAULT_GROUP"
128bool(true)
129%s(24) "MYSQLI_READ_DEFAULT_FILE"
130bool(true)
131%s(26) "MYSQLI_OPT_CONNECT_TIMEOUT"
132bool(true)
133%s(23) "MYSQLI_OPT_LOCAL_INFILE"
134bool(true)
135%s(19) "MYSQLI_INIT_COMMAND"
136bool(true)
137%s(17) "MYSQLI_CLIENT_SSL"
138bool(false)
139Link closed
140Warning: mysqli_options(): Couldn't fetch mysqli in %s line %d
141%s(19) "MYSQLI_INIT_COMMAND"
142NULL
143done!
144