MariaDB – Select Database

MariaDB – Select Database

After connecting to MariaDB, you must select a database to work with because many databases may exist. There are two ways to perform this task: from the command prompt or through a PHP script.

The Command Prompt

In choosing a database at the command prompt, simply utilize the SQL command ‘use’

[root@host]# mysql -u root -p

Enter password:******

mysql> use PRODUCTS;

Database changed

mysql> SELECT database();  
| Database                | 
| PRODUCTS                | 

Once you select a database, all subsequent commands will operate on the chosen database.

Note − All names (e.g., database, table, fields) are case sensitive. Ensure commands conform to the proper case.

PHP Select Database Script

PHP provides the mysql_select_db function for database selection. The function uses two parameters, one optional, and returns a value of “true” on successful selection, or false on failure.


Review the following select database script syntax.

bool mysql_select_db( db_name, connection );

The description of the parameters is given below −

S.No Parameter & Description
1 db_name

This required parameter specifies the name of the database to use.

2 connection

When not specified, this optional parameter uses the most recent connection used.

Try the following example code for selecting a database −

      <title>Select a MariaDB Database</title>

         $dbhost = 'localhost:3036';
         $dbuser = 'guest1';
         $dbpass = 'guest1a';
         $conn = mysql_connect($dbhost, $dbuser, $dbpass);
         if(! $conn ) {
            die('Could not connect: ' . mysql_error());
         echo 'Connected successfully';
         mysql_select_db( 'PRODUCTS' );

On successful selection, you will see the following output −

mysql> Connected successfully

Was this article helpful?

Related Articles

Leave A Comment?

You must be logged in to post a comment.