Easy Tutorial
❮ Mysql Select Query Mysql Temporary Tables ❯

MySQL Selecting a Database

After connecting to the MySQL database, you may have multiple databases available for operation, so you need to select the database you want to work with.


Selecting a MySQL Database from the Command Prompt

Selecting a specific database in the mysql> prompt is straightforward. You can use an SQL command to select the specified database.

Example

The following example selects the database tutorialpro:

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

After executing the above command, you have successfully selected the tutorialpro database, and all subsequent operations will be performed within the tutorialpro database.


Selecting a MySQL Database Using PHP Script

PHP provides the function mysqli_select_db to select a database. The function returns TRUE on success and FALSE on failure.

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:

Selecting a Database

<?php
$dbhost = 'localhost';  // mysql server host address
$dbuser = 'root';       // mysql username
$dbpass = '123456';     // mysql user password
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
    die('Connection failed: ' . mysqli_error($conn));
}
echo 'Connection successful';
mysqli_select_db($conn, 'tutorialpro' );
mysqli_close($conn);
?>
❮ Mysql Select Query Mysql Temporary Tables ❯