After you connect to the MySQL database, there may be more than one database to operate on, so you need to select the database you want to operate on.


Select the MySQL database from the command prompt window

Select a specific database easily in the mysql> prompt window. You can use SQL commands to select the specified database.

The instance

The following example selects the database RUNOOB:

[root@host]# mysql -u root -p
Enter password:******
mysql> use RUNOOB;
Database changed
mysql>
Copy the code

After executing the above command, you have successfully selected the RUNOOB database, and the subsequent operations will be executed in the RUNOOB 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.


Select MySQL database using PHP script

PHP provides the function mysqli_select_DB to select a database. The function returns TRUE on success, FALSE otherwise.

grammar

mysqli_select_db(connection,dbname);
Copy the code
parameter describe
connection A necessity. Specify the MySQL connection to use.
dbname Required, specifies the default database to use.

The instance

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

Select database

<? php $dbhost = ‘localhost’; $dbuser = ‘root’; $dbpass = ‘123456’; $conn = mysqli_connect($dbuser, $dbpass); if(! $conn) {die(‘ connection failed: ‘. Mysqli_error ($conn)); } echo ‘connect successfully ‘; mysqli_select_db($conn, ‘RUNOOB’ ); mysqli_close($conn); ? >