1--TEST--
2mysql_select_db()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7?>
8--FILE--
9<?php
10include_once "connect.inc";
11
12$tmp    = NULL;
13$link   = NULL;
14
15if (false !== ($tmp = @mysql_select_db($link)))
16	printf("[001] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
17
18if (!$link = my_mysql_connect($host, $user, $passwd, $db, $port, $socket))
19	printf("[002] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
20		$host, $user, $db, $port, $socket);
21
22if (!is_null($tmp = @mysql_select_db($db, $link, "foo")))
23	printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
24
25/* does not make too much sense, unless we have access to at least one more database than $db */
26if (!mysql_select_db($db, $link))
27	printf("[004] Cannot select DB %s, [%d] %s\n", $db, mysql_errno($link), mysql_error($link));
28
29if (!$res = mysql_query("SELECT DATABASE() AS dbname", $link))
30	printf("[005] [%d] %s\n", mysql_errno($link), mysql_error($link));
31
32if (!$row = mysql_fetch_assoc($res))
33	printf("[006] [%d] %s\n", mysql_errno($link), mysql_error($link));
34
35if ($row['dbname'] !== (string)$db)
36	printf("[007] Expecting database '%s', found '%s'\n", $db, $row['dbname']);
37
38var_dump($row['dbname']);
39
40mysql_free_result($res);
41
42if (mysql_select_db('mysql', $link)) {
43	// Yippie, a second database to play with - that's great because mysql_select_db
44	// ($db) was done by mysql__connect() already and the previous test
45	// was quite useless
46	if (!$res = mysql_query("SELECT DATABASE() AS dbname", $link))
47		printf("[008] [%d] %s\n", mysql_errno($link), mysql_error($link));
48
49	if (!$row = mysql_fetch_assoc($res))
50		printf("[009] [%d] %s\n", mysql_errno($link), mysql_error($link));
51
52	if (strtolower($row['dbname']) !== 'mysql')
53		printf("[010] Expecting database 'mysql', found '%s'\n", $row['dbname']);
54
55	mysql_free_result($res);
56}
57
58
59var_dump(mysql_select_db('I can not imagine that this database exists', $link));
60
61mysql_close($link);
62
63if (false !== ($tmp = mysql_select_db($db, $link)))
64	printf("[012] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
65
66print "done!\n";
67?>
68--EXPECTF--
69Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
70%unicode|string%(%d) "%s"
71bool(false)
72
73Warning: mysql_select_db(): %d is not a valid MySQL-Link resource in %s on line %d
74done!
75