1--TEST--
2new mysqli()
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	$tmp    = NULL;
14	$link   = NULL;
15
16	$obj = new stdClass();
17
18	if ($mysqli = new mysqli($host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket) && !mysqli_connect_errno())
19		printf("[003] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n",
20			$host, $user . 'unknown_really', $db, $port, $socket);
21
22	if (false !== $mysqli)
23		printf("[004] Expecting boolean/false, got %s/%s\n", gettype($mysqli), $mysqli);
24
25	// Run the following tests without an anoynmous MySQL user and use a password for the test user!
26	ini_set('mysqli.default_socket', $socket);
27	if (!is_object($mysqli = new mysqli($host, $user, $passwd, $db, $port)) || (0 !== mysqli_connect_errno())) {
28		printf("[005] Usage of mysqli.default_socket failed\n") ;
29	} else {
30		$mysqli->close();
31	}
32
33	ini_set('mysqli.default_port', $port);
34	if (!is_object($mysqli = new mysqli($host, $user, $passwd, $db)) || (0 !== mysqli_connect_errno())) {
35		printf("[006] Usage of mysqli.default_port failed\n") ;
36	} else {
37		$mysqli->close();
38	}
39
40	ini_set('mysqli.default_pw', $passwd);
41	if (!is_object($mysqli = new mysqli($host, $user)) || (0 !== mysqli_connect_errno())) {
42		printf("[007] Usage of mysqli.default_pw failed\n") ;
43	} else {
44		$mysqli->close();
45	}
46
47	ini_set('mysqli.default_user', $user);
48	if (!is_object($mysqli = new mysqli($host)) || (0 !== mysqli_connect_errno())) {
49		printf("[008] Usage of mysqli.default_user failed\n") ;
50	} else {
51		$mysqli->close();
52	}
53
54	ini_set('mysqli.default_host', $host);
55	if (!is_object($mysqli = new mysqli()) || (0 !== mysqli_connect_errno())) {
56		printf("[012] Failed to create mysqli object\n");
57	} else {
58		// There shall be NO connection! Using new mysqli(void) shall not use defaults for a connection!
59			// We had long discussions on this and found that the ext/mysqli API as
60			// such is broken. As we can't fix it, we document how it has behaved from
61			// the first day on. And that's: no connection.
62			if (NULL !== ($tmp = @$mysqli->query('SELECT 1'))) {
63				printf("[013] There shall be no connection!\n");
64				$mysqli->close();
65			}
66	}
67
68	if ($IS_MYSQLND) {
69		ini_set('mysqli.default_host', 'p:' . $host);
70		if (!is_object($mysqli = new mysqli())) {
71			// Due to an API flaw this shall not connect
72			printf("[010] Failed to create mysqli object\n");
73		} else {
74			// There shall be NO connection! Using new mysqli(void) shall not use defaults for a connection!
75			// We had long discussions on this and found that the ext/mysqli API as
76			// such is broken. As we can't fix it, we document how it has behaved from
77			// the first day on. And that's: no connection.
78			if (NULL !== ($tmp = @$mysqli->query('SELECT 1'))) {
79				printf("[011] There shall be no connection!\n");
80				$mysqli->close();
81			}
82		}
83	}
84
85	print "... and now Exceptions\n";
86	mysqli_report(MYSQLI_REPORT_OFF);
87	mysqli_report(MYSQLI_REPORT_STRICT);
88
89	try {
90		$mysqli = new mysqli($host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket);
91		printf("[016] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n",
92			$host, $user . 'unknown_really', $db, $port, $socket);
93		$mysqli->close();
94	} catch (mysqli_sql_exception $e) {
95		printf("%s\n", $e->getMessage());
96	}
97
98	ini_set('mysqli.default_socket', $socket);
99	try {
100		$mysqli = new mysqli($host, $user, $passwd, $db, $port);
101		$mysqli->close();
102	} catch (mysqli_sql_exception $e) {
103		printf("%s\n", $e->getMessage());
104		printf("[017] Usage of mysqli.default_socket failed\n") ;
105	}
106
107	ini_set('mysqli.default_port', $port);
108	try {
109		$mysqli = new mysqli($host, $user, $passwd, $db);
110		$mysqli->close();
111	} catch (mysqli_sql_exception $e) {
112		printf("%s\n", $e->getMessage());
113		printf("[018] Usage of mysqli.default_port failed\n") ;
114	}
115
116	ini_set('mysqli.default_pw', $passwd);
117	try {
118		$mysqli = new mysqli($host, $user);
119		$mysqli->close();
120	} catch (mysqli_sql_exception $e) {
121		printf("%s\n", $e->getMessage());
122		printf("[019] Usage of mysqli.default_pw failed\n");
123	}
124
125	ini_set('mysqli.default_user', $user);
126	try {
127		$mysqli = new mysqli($host);
128		$mysqli->close();
129	} catch (mysqli_sql_exception $e) {
130		printf("%s\n", $e->getMessage());
131		printf("[020] Usage of mysqli.default_user failed\n") ;
132	}
133
134	ini_set('mysqli.default_host', $host);
135	try {
136		/* NOTE that at this point one must use a different syntax! */
137		$mysqli = mysqli_init();
138		$mysqli->real_connect();
139		assert(0 === mysqli_connect_errno());
140		$mysqli->close();
141		assert(0 === mysqli_connect_errno());
142	} catch (mysqli_sql_exception $e) {
143		printf("%s\n", $e->getMessage());
144		printf("[021] Usage of mysqli.default_host failed\n");
145	}
146
147	print "done!";
148?>
149--EXPECTF--
150Warning: mysqli::mysqli(): (%s/%d): Access denied for user '%sunknown%s'@'%s' (using password: %s) in %s on line %d
151... and now Exceptions
152Access denied for user '%s'@'%s' (using password: %s)
153done!