We now have a youtube channel. Subscribe!

PHP | mysqli_select_db() Function

PHP mysqli_select_db() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mysqli_select_db() Function.

The built-in mysqli_select_db() function in PHP accepts a string value that represents an existing database and, makes it as the default database.

Syntax

Following below is the syntax to use this function -

mysqli_select_db($con  name)


Parameter Details

Sr.NoParameter & 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 Value

This built-in PHP function returns true if the operation is successful otherwise, false on failure.

PHP Version

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

Example1

The following example demonstrates the usage of the built-in PHP 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);
?>

Output

When the above code is executed, it will produce the following result -

Current Database: newdatabase

Example2

In an object oriented style the syntax of this built-in function is $con->select_db(); The following below is an example of this PHP function in an 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();
?>

Output

When the above code is executed, it will produce the following result -

Initial Database: mydb
Current Database: newdatabase

Example3

Instead of specifying the database at the time of connection, you can also choose it later using this built-in 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, 'Kennedy', 'Nkpara', 'PortHarcourt', 'Nigeria')");
   mysqli_query($con, "insert into my_team values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   mysqli_query($con, "insert into my_team values(3, 'Queen', 'Dauglas', 'Texas', 'UnitedStates')");
   mysqli_query($con, "insert into my_team values(4, 'Paul', 'Francis', 'Texas', 'UnitedStates')");

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

Output

When the above code is executed, it will produce the following result below -

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

Example4

Try the following example below -

<?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);

?>

Output

When the above code is executed, it will produce the following result below -

Database Selected


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the mysqli_set_charset() Function in PHP.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain