English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MySQL Select Database

After you connect to the MySQL database, there may be multiple databases available for operation, so you need to select the database you want to operate.

Select MySQL database from the command prompt

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

Online Example

The following examples select the database w3codebox:

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

After executing the above command, you have successfully selected w3The codebox database, in subsequent operations, will be in the w3Executed in the codebox database.

Note:All database names, table names, and table fields are case-sensitive. Therefore, you need to enter the correct names when using SQL commands.

Use PHP script to select MySQL database

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

Syntax

mysqli_select_db(connection,dbname);
ParametersDescription
connectionRequired. Specify the MySQL connection to use.
dbnameRequired, specify the default database to use.

Online Example

The following example demonstrates how to use the mysqli_select_db function to select a database:

<?php
$dbhost = 'localhost';  // MySQL Server Host Address
$dbuser = 'root';            // MySQL Username
$dbpass = '123456';          // MySQL Username and Password
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
    die('Connection Failed: ' . mysqli_error($conn));
}
echo 'Connection Successful';
mysqli_select_db($conn, 'w3codebox' );
mysqli_close($conn);
?>