(PHP 4, PHP 5)
mysql_select_db — 选择 MySQL 数据库
$database_name
[, resource $
link_identifier
] )
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
mysql_select_db() 设定与指定的连接标识符所关联的服务器上的当前激活数据库。如果没有指定连接标识符,则使用上一个打开的连接。如果没有打开的连接,本函数将无参数调用 mysql_connect() 来尝试打开一个并使用之。
每个其后的 mysql_query() 调用都会作用于活动数据库。
Example #1 mysql_select_db() 例子
<?php
$lnk = mysql_connect('localhost', 'mysql_user', 'mysql_password')
or die ('Not connected : ' . mysql_error());
// make foo the current db
mysql_select_db('foo', $lnk) or die ('Can\'t use foo : ' . mysql_error());
?>
参见 mysql_connect(), mysql_pconnect() 和 mysql_query()。
为向下兼容仍然可以使用 mysql_selectdb(),但反对这样做。
database_name
The name of the database that is to be selected.
link_identifier
MySQL
连接。如不指定连接标识,则使用由 mysql_connect()
最近打开的连接。如果没有找到该连接,会尝试不带参数调用
mysql_connect()
来创建。如没有找到连接或无法建立连接,则会生成
E_WARNING
级别的错误。
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Example #2 mysql_select_db() example
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
?>
Note:
为了向下兼容,可以使用下列已废弃的别名: mysql_selectdb()
miloshio at gmail dot com (2012-05-05 17:28:00)
You can select MySQL database without using this function.
Simply right after connecting to MySQL
<?php $connection = mysql_connect("dabatbasehost", "username", "password"); ?>
perform this query:
<?php mysql_query("USE somedatabase", $connection); ?>
duncan at berrimans dot co dot uk (2012-04-20 14:11:42)
Note that the manual is slightly misleading it states :-
"Sets the current active database on the server that's associated with the specified link identifier. Every subsequent call to mysql_query() will be made on the active database."
The 2nd statement is not true or at best unclear.
mysql_query() manual entry actually correctly states it will use the last link opened by mysql_connect() by default.
Thus if you have 2 connections you will need to specify the connection when calling mysql_query or issue the connect again to ensure the 1st database becomes the default, simply using mysql_select_db will not make the 1st database the default for subsequent calls to mysql_query.
Its probably only apparent when the two databases are on different servers.
riad93 at mail dot ru (2009-09-12 04:44:25)
You can use DataBases without <?php mysql_select_db() ?>
And you will havenot james at gogo dot co dot nz's problems :)
<?php
mysql_connect('localhost','db_user','pssword');
mysql_query('SELECT * FROM database_name.table_name');
?>
anotheruser at example dot com (2008-08-12 16:57:57)
Cross-database join queries, expanding on Dan Ross's post...
Really, this is a mysql specific feature, but worth noting here. So long as the mysql user has been given the right permissions to all databases and tables where data is pulled from or pushed to, this will work. Though the mysql_select_db function selects one database, the mysql statement may reference another (the syntax for referencing a field in another db table being 'database.table.field').
<?php
$sql_statement = "SELECT
PostID,
AuthorID,
Users.tblUsers.Username
FROM tblPosts
LEFT JOIN Users.tblUsers ON AuthorID = Users.tblUsers.UserID
GROUP BY PostID,AuthorID,Username
";
$dblink = mysql_connect("somehost", "someuser", "password");
mysql_select_db("BlogPosts",$dblink);
$qry = mysql_query($sql_statement,$dblink);
?>
me at khurshid dot com (2007-09-08 23:03:08)
Problem with connecting to multiple databases within the same server is that every time you do:
mysql_connect(host, username, passwd);
it will reuse 'Resource id' for every connection, which means you will end with only one connection reference to avoid that do:
mysql_connect(host, username, passwd, true);
keeps all connections separate.
Maarten (2005-08-19 05:09:38)
About opening connections if the same parameters to mysql_connect() are used: this can be avoided by using the 'new_link' parameter to that function.
This parameter has been available since PHP 4.2.0 and allows you to open a new link even if the call uses the same parameters.
buzz at oska dot com (2005-05-06 05:39:17)
Opening multiple connection handles with:
<?php
$connection_handle = mysql_connect($hostname_and_port,$user,$password);
?>
causes the connection ID/handle to be REUSED if the exact same parameters are passed in to it. This can be annoying if you want to work with multiple databases on the same server, but don't want to (a) use the database.table syntax in all your queries or (b) call the mysql_select_db($database) before every SQL query just to be sure which database you are working with.
My solution is to create a handle for each database with mysql_connect (using ever so slightly different connection properties), and assign each of them to their own database permanently. each time I do a mysql_query(...) call, I just include the connection handle that I want to do this call on eg (ive left out all error checking for simplicity sake):
<?php
// none of thesehandles are re-used as the connection parameters are different on them all, despite connecting to the same server (assuming 'myuser' and 'otheruser' have the same privileges/accesses in mysql)
$handle_db1 = mysql_connect("localhost","myuser","apasswd");
$handle_db2 = mysql_connect("127.0.0.1","myuser","apasswd");
$handle_db3 = mysql_connect("localhost:3306","myuser","apasswd");
$handle_db4 = mysql_connect("localhost","otheruser","apasswd");
// give each handle it's own database to work with, permanently.
mysql_select_db("db1",$handle_db1);
mysql_select_db("db2",$handle_db2);
mysql_select_db("db3",$handle_db3);
mysql_select_db("db4",$handle_db4);
//do a query from db1:
$query = "select * from test"; $which = $handle_db1;
mysql_query($query,$which);
//do a query from db2 :
$query = "select * from test"; $which = $handle_db2;
mysql_query($query,$which);
//etc
?>
Note that we didn't do a mysql_select_db between queries , and we didn't use the database name in the query either.
Of course, it has the overhead of setting up an extra connection.... but you may find this is preferable in some cases...
james at gogo dot co dot nz (2004-01-16 16:45:07)
Be carefull if you are using two databases on the same server at the same time. By default mysql_connect returns the same connection ID for multiple calls with the same server parameters, which means if you do
<?php
$db1 = mysql_connect(...stuff...);
$db2 = mysql_connect(...stuff...);
mysql_select_db('db1', $db1);
mysql_select_db('db2', $db2);
?>
then $db1 will actually have selected the database 'db2', because the second call to mysql_connect just returned the already opened connection ID !
You have two options here, eiher you have to call mysql_select_db before each query you do, or if you're using php4.2+ there is a parameter to mysql_connect to force the creation of a new link.
doug at xamo dot com (2003-12-17 12:39:03)
When you need to query data from multiple databases, note that mysql_select_db("db2") doesn't prevent you from fetching more rows with result sets returned from "db1".
<?php
mysql_select_db("db1");
$res_db1=mysql_query("select * from foobar");
myqsl_select_db("db2);
$row_db1=mysql_fetch_object($res_db1);
$res_db2=mysql_query("select * from test where id='$row_db1->id'");
?>