Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

MySQL selects the database


May 15, 2021 MySQL


Table of contents


MySQL selects the database

After you connect to the MySQL database, there may be multiple operational databases, so you need to select the database you want to operate on.


Select the MySQL database from the command prompt window

It is easy to select a specific database in the mysql?prompt window. You can use the SQL command to select the specified database.

Instance

The following example selects the database W3CSCHOOL:

[root@host]# mysql -u root -p
Enter password:******
mysql> use W3CSCHOOL;
Database changed
mysql>

After you execute the above commands, you have successfully selected the W3CSCHOOL database, which will be executed in the W3CSCHOOL database in subsequent operations.

Note: All database names, table names, and table fields are case sensitive. So you need to enter the correct name when using sql commands.


Use the PHP script to select the MySQL database

PHP provides a mysql_select_db to select a database. The function returns TRUE after successful execution, otherwise false is returned.

Grammar

bool mysql_select_db( db_name, connection );
Parameters Describe
db_name Necessary. Specify the database to select.
connection Optional. T he MySQL connection is specified. If not specified, the last connection is used.

Instance

The following example shows how mysql_select_db to select a database using the function:

<html>
<head>
<meta charset="utf-8"> 
<title>选择 MySQL 数据库</title>
</head>
<body>
<?php
$dbhost = 'localhost:3036';
$dbuser = 'guest';
$dbpass = 'guest123';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('连接失败: ' . mysql_error());
}
echo '连接成功';
mysql_select_db($conn, 'W3CSCHOOL' );
mysql_close($conn);
?>
</body>
</html>