MySQLi - Select Database


Advertisements

Once you get connection with MySQLi server, it is required to select a particular database to work with. This is because there may be more than one database available with MySQLi Server.

Syntax

bool mysqli_select_db( db_name, connection );

Sr.No. Parameter & Description
1

db_name

Required - MySQLi Database name to be selected

2

connection

Optional - if not specified, then last opened connection by mysqli_connect will be used.

Example

Here is the example showing you how to select a database.

<html>
   <head>
      <title>Connecting MySQLi Server</title>
   </head>

   <body>
      <?php
         $dbhost = 'localhost:3306';
         $dbuser = 'root';
         $dbpass = '<Password Here>';
         $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
   
         if(! $conn ) {
            echo 'Connected failure<br>';
         }
         echo 'Connected successfully<br>';
         mysqli_select_db( 'TUTORIALS' );
   
         //Write some code here
         mysqli_close($conn);
      ?>
   </body>
</html>

If connected and Selected database successfully,then the sample output should be like this −

Connected successfully
Advertisements