mysql_select_db Function in PHP

What is mysql_select_db() function in PHP?
How does mysql_select_db works?


mysql_select_db() function sets the active MySQL database.
bool mysql_select_db ( string database_name [, resource link_identifier])

mysql_select_db() attempts to select existing database on the server associated with the specified link identifier.
Returns TRUE on success, or FALSE on failure.
We can select database in mysql server by using mysql_select_db function. mysql_select_db() selects the current active database on the server that is associated with the specified link identifier. If no link identifier is specified, the last opened link is assumed. If no link is open, the function will try to set a link as if mysql_connect() was called without arguments.

// Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");
// select current db
$db_selected = mysql_select_db('mydatabase', $link);
if (!$db_selected) {
die ('Can't select : ' . mysql_error());

In this script, replace "mysql_host", "mysql_user" and "mysql_password" with your mySQL login and password supplied by the information which you set when you installed mySQL.
Function mysql_select_db("mydatabase",$link) will select existing database with the database name passed as an argument along with the specified link identifier.
See also:
mysql_connect(), mysql_pconnect() and mysql_query().

PHP Topics

Ask Questions

Ask Question