1--TEST--
2mysqli_select_db()
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	require_once("table.inc");
13
14	$tmp    = NULL;
15	$link   = NULL;
16
17	if (!is_null($tmp = @mysqli_select_db()))
18		printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
19
20	if (!is_null($tmp = @mysqli_select_db($link)))
21		printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
22
23	if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
24		printf("[003] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
25			$host, $user, $db, $port, $socket);
26
27	if (!is_null($tmp = @mysqli_select_db($link, $db, "foo")))
28		printf("[004] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
29
30	/* does not make too much sense, unless we have access to at least one more database than $db */
31	if (!mysqli_select_db($link, $db))
32		printf("[005] Cannot select DB %s, [%d] %s\n", $db, mysqli_errno($link), mysqli_error($link));
33
34	if (!$res = mysqli_query($link, "SELECT DATABASE() AS dbname"))
35		printf("[006] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
36
37	if (!$row = mysqli_fetch_assoc($res))
38		printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
39
40	if ($row['dbname'] !== (string)$db)
41		printf("[008] Expecting database '%s', found '%s'\n", $db, $row['dbname']);
42	mysqli_free_result($res);
43
44	if (mysqli_select_db($link, 'mysql')) {
45		// Yippie, a second database to play with - that's great because mysqli_select_db
46		// ($db) was done by mysqli__connect() already and the previous test
47		// was quite useless
48		if (!$res = mysqli_query($link, "SELECT DATABASE() AS dbname"))
49			printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
50
51		if (!$row = mysqli_fetch_assoc($res))
52			printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
53
54		if (strtolower($row['dbname']) !== 'mysql')
55			printf("[011] Expecting database 'mysql', found '%s'\n", $row['dbname']);
56		mysqli_free_result($res);
57	}
58
59	if (!$link->select_db($db))
60		printf("[012] Failed to set '%s' as current DB; [%d] %s\n", $link->errno, $link->error);
61
62	if (!$res = mysqli_query($link, "SELECT DATABASE() AS dbname"))
63		printf("[013] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
64
65	if (!$row = mysqli_fetch_assoc($res))
66		printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
67
68	$current_db = $row['dbname'];
69
70	mysqli_report(MYSQLI_REPORT_OFF);
71	mysqli_select_db($link, 'I can not imagine that this database exists');
72	mysqli_report(MYSQLI_REPORT_ERROR);
73
74	ob_start();
75	mysqli_select_db($link, 'I can not imagine that this database exists');
76	$output = ob_get_contents();
77	ob_end_clean();
78	if (!stristr($output, "1049") && !stristr($output, "1044") && !stristr($output, "1045")) {
79	  /* Error: 1049 SQLSTATE: 42000 (ER_BAD_DB_ERROR) Message: Unknown database '%s'  */
80	  /* Error: 1044 SQLSTATE: 42000 (ER_DBACCESS_DENIED_ERROR) Message: Access denied for user '%s'@'%s' to database '%s' */
81	  /* Error: 1045 SQLSTATE: 28000 (ER_ACCESS_DENIED_ERROR) Message: Access denied for user '%s'@'%s' (using password: %s) */
82	  echo $output;
83	}
84
85	if (!$res = mysqli_query($link, "SELECT DATABASE() AS dbname"))
86		printf("[015] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
87
88	if (!$row = mysqli_fetch_assoc($res))
89		printf("[016] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
90
91	if (strtolower($row['dbname']) != strtolower($current_db))
92		printf("[017] Current DB should not change if set fails\n");
93
94
95	if (!$res = $link->query("SELECT id FROM test WHERE id = 1"))
96		printf("[018] [%d] %s\n");
97
98	$row = $res->fetch_assoc();
99	$res->free();
100
101	mysqli_close($link);
102
103	if (NULL !== ($tmp = mysqli_select_db($link, $db)))
104		printf("[017] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
105
106	print "done!\n";
107?>
108--CLEAN--
109<?php require_once("clean_table.inc"); ?>
110--EXPECTF--
111Warning: mysqli_select_db(): Couldn't fetch mysqli in %s on line %d
112done!
113