• PHP Video Tutorials

PHP mysqli_select_db() Function



Definition and Usage

The mysqli_select_db() function accepts a string value representing an existing database and, makes it as a the default database.

Syntax

mysqli_select_db($con, name)

Parameters

Sr.No Parameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.

2

name(Mandatory)

This is a string value representing the name of an existing database which you need to make as the default database.

Return Values

The PHP mysqli_select_db() function returns a boolean value which is, true if the operation is successful and, false if not.

PHP Version

This function was first introduced in PHP Version 5 and works works in all the later versions.

Example

Following example demonstrates the usage of the mysqli_select_db() function (in procedural style) −

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   //Selecting the database
   mysqli_query($con, "CREATE DATABASE NewDatabase");
   mysqli_select_db($con, "NewDatabase");

   //Retrieving the current database name
   $res = mysqli_query($con, "SELECT DATABASE()");

   while ($row = mysqli_fetch_row($res)) {
      print("Current Database: ".$row[0]);
   }

   //Closing the connection
   mysqli_close($con);
?>

This will produce following result −

Current Database: newdatabase

Example

In object oriented style the syntax of this function is $con->select_db(); Following is the example of this function in object oriented style $minus;

<?php
   //Creating a connection
   $con = new mysqli("localhost", "root", "password", "mydb");

   //Retrieving the current database name
   $res = $con->query("SELECT DATABASE()");
   while ($row = $res->fetch_row()) {
      print("Initial Database: ".$row[0]."\n");
   }

   //Selecting the database
   $con->query("CREATE DATABASE NewDatabase");
   $con->select_db("NewDatabase");

   //Retrieving the current database name
   $res = $con->query("SELECT DATABASE()");

   while ($row = $res->fetch_row()) {
      print("Current Database: ".$row[0]);
   }

   //Closing the connection
   $res = $con -> close();
?>

This will produce following result −

Initial Database: mydb
Current Database: newdatabase

Example

Instead of specifying the database at the time of connection, you can also choose it later using this function as shown below −

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password");

   //Selecting the database
   mysqli_select_db($con, "mydb");
   print("Database Selected ..."."\n");

   mysqli_query($con, "CREATE TABLE IF NOT EXISTS my_team(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
   print("Table Created ..."."\n");

   //Inserting a records into the my_team table
   mysqli_query($con, "insert into my_team values(1, 'Shikhar', 'Dhawan', 'Delhi', 'India')");
   mysqli_query($con, "insert into my_team values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   mysqli_query($con, "insert into my_team values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')");
   mysqli_query($con, "insert into my_team values(4, 'Virat', 'Kohli', 'Delhi', 'India')");

   print("Records Inserted ..."."\n");
 
   //Closing the connection
   mysqli_close($con);
?>

This will produce following result −

Database Selected ...
Table Created ...
Records Inserted ...

Example

<?php
   $connection_mysql = mysqli_connect("localhost", "root", "password","mydb");
   
   if (mysqli_connect_errno($connection_mysql)){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
   
   $res = mysqli_select_db($connection_mysql,"testdb");
   
   if($res){
	   echo "Database Selected";
   }else{
	   echo "Error Occurred";
   }
   
   mysqli_close($connection_mysql);

?>

This will produce following result −

Database Selected
php_function_reference.htm
Advertisements