MySQL select database
MySQL Select Database
After you connect to the MySQL database, there may be multiple databases that can be operated, so you need to select the database you want to operate. .
1. 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 SQL commands to select a specific database.
Example
The following example selects the database DEMO:
[root@host]# mysql -u root -p Enter password:****** mysql> use RUNOOB; Database changed mysql>
After executing the above command, you have successfully selected the DEMO database. In the subsequent All operations will be performed in the DEMO database.
Note: All database names, table names, and table fields are case-sensitive. So you need to enter the correct name when using SQL commands.
2. Use PHP script to select MySQL database
PHP provides the function mysqli_select_db to select a database. The function returns TRUE if executed successfully, otherwise it returns FALSE.
Syntax
mysqli_select_db(connection,dbname);
Parameters | Description |
connection | Required. Specifies the MySQL connection to use. |
dbname | Required, specifies the default database to be used. |
Example
The following example shows how to use the mysqli_select_db function to select a database:
<?php header("Content-Type: text/html;charset=utf-8"); $dbhost = 'localhost'; // mysql服务器主机地址 $dbuser = 'root'; // mysql用户名 $dbpass = 'root'; // mysql用户名密码 $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('连接失败: ' . mysqli_error($conn)); } echo '连接成功'; mysqli_select_db($conn, 'DEMO' ); mysqli_close($conn); ?>
Related video recommendations: mysql tutorial