PHP mysql_select_db function

 The function of the PHP mysql_select_db function is to search the MySQL database. Before using mysql_select_db, you usually use mysql_connect to create a MySQL database link, including the login and authentication MySQL account password. After the link is created, the mysql_select_db function can Among them, select the database we want.


Basic syntax of PHP mysql_select_db function
mysql_select_db ( $database_name , $link_identifier )
The PHP mysql_select_db function returns TRUE if it succeeds, and FALSE if it fails.

PHP's mysql_select_db has two parameters. The first parameter $database_name refers to the database name and necessary items, and the second parameter $link_identifier is the MySQL database link. You can use the method mentioned in the first paragraph, first use mysql_connect Create a MySQL database link, and then put it in the position of the second parameter. If it is not filled in, the mysql_select_db function will automatically search for the database link created by the last mysql_connect . If it cannot find it, it will call it by itself mysql_connect is used to establish a connection. If it still cannot be established, an E_WARNING level error will be generated. Therefore, it is recommended to write the connection yourself for completeness.

The database selected by the mysql_select_db function can be used by other php scripts behind the same PHP program.

PHP mysql_select_db function example
 <?php
$lnk = mysql_connect('localhost','mysql_user','mysql_password')
 or die ('Unable to connect to database');

mysql_select_db ('test_database', $lnk) or die ('Unable to select database:' . mysql_error());
?>
The example first uses the mysql_connect function to establish a database connection, and then you can search the "test_database" database through mysql_select_db. If it succeeds, it returns TRUE, and if it fails, it returns FALSE to display an error message through mysql_error .

Post a Comment

0 Comments