Function name: mysql_select_db()
Applicable version: PHP 5.x - PHP 7.x (deprecated)
Function Description: The mysql_select_db() function is used to select a MySQL database so that subsequent database operations are performed in the database.
Syntax: bool mysql_select_db ( string $database_name [, resource $link_identifier = NULL ] )
parameter:
- $database_name: The database name to select.
- $link_identifier (optional): Database connection identifier. If this parameter is not provided, the most recently opened database connection is used by default.
Return value: Return true on success, and false on failure.
Example:
- Connect to the MySQL database and select the database:
$link = mysql_connect('localhost', 'username', 'password'); if (!$link) { die('连接数据库失败:' . mysql_error()); } $db_selected = mysql_select_db('database_name', $link); if (!$db_selected) { die('选择数据库失败:' . mysql_error()); } echo '成功选择数据库!';
- Use the default connection when selecting a database:
$db_selected = mysql_select_db('database_name'); if (!$db_selected) { die('选择数据库失败:' . mysql_error()); } echo '成功选择数据库!';
Notes:
- The mysql_select_db() function has been deprecated and is not recommended for use in new projects. It is recommended to use mysqli or PDO extensions for database operations.
- Before using the mysql_select_db() function, you must first establish a database connection using the mysql_connect() or mysql_pconnect() function.
- After selecting a database using the mysql_select_db() function, subsequent database operations will be performed in that database until another database is selected or the connection is closed.
- If the database selection fails, you can use the mysql_error() function to obtain error information for debugging.
- In PHP 7.x version, the mysql_select_db() function has been removed and the database operation should be performed using the corresponding functions in the mysqli or PDO extension instead.