PHP mysqli_select_db() Function

PHP mysqli_select_db() Function is used to choose database for the connection. Also You can select the default database using 4th parameter in mysqli_connect() function.

PHP Version : PHP 5, PHP 7

Syntax for mysqli_select_db:

According to Procedural

mysqli_select_db($connection, $database_name);

According to Object oriented

$mysqli -> select_db($database_name);
 
Parameter,
$connection : Required. It is MySQL connection object which is return by mysqli_connect() function.
$database_name : Required. It is database name.
 

Return values for mysqli_connect:

It returns TRUE on success and  FALSE on failure.

Example for mysqli_connect:

Let's see below example to understand php mysqli_select_db() Function in details.

Example :

<?php
$database_name="test";
$connection = @mysqli_connect("localhost","username","password");
//check connection.
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL server : " . mysqli_connect_error();
exit();
}
@mysqli_select_db($connection, $database_name); // choose database from mysql server
?>

Output :

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

94167