MySQL Select Database
After you connect to the MySQL database, there may be multiple databases you can operate on, so you need to select the database you want to use.
Select a MySQL database from the command prompt window
Inmysql>You can easily select a specific database in the prompt window.
In MySQL, to select the database to use, you can useUSEstatement, the basic syntax is as follows:
USE database_name;
Parameter description:
- database_nameis the name of the database you want to select.
After selecting the database, your subsequent SQL queries and operations will be executeddatabase_nameon the specified database.
Example
The following example selects the database EXAMPLE:
[root@host]# mysql -u root -p Enter password:****** mysql> use EXAMPLE; Database changed mysql>
After executing the above command, you have successfully selected the EXAMPLE database, and all subsequent operations will be executed in the EXAMPLE database.
On the command line, you can select a database in the following way:
mysql -u your_username -p -D your_database
- -DThe parameter is used to specify the database to be selected.
For example:
mysql -u root -p -D EXAMPLE
After entering the password, you will enter the MySQL prompt, and any subsequent queries and operations will be executed on the EXAMPLE database.
Make sure the database you select exists, otherwise you will receive an error message. You can useSHOW DATABASES;to query the available databases, and make sure the database you want to select is in the list.
Select MySQL database using PHP script
PHP provides the function mysqli_select_db to select a database. The function returns TRUE on success, otherwise returns FALSE.
Syntax
mysqli_select_db(connection,dbname);
| Parameter | Description |
|---|---|
| connection | Required. Specifies the MySQL connection to use. |
| dbname | Required. Specifies the default database to use. |
Example
The following example demonstrates how to use the mysqli_select_db function to select a database: