English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_select_db() function is used to change the default database for the connection.
mysqli_select_db()The function accepts a string representing the existing database and uses it as the default database.
mysqli_select_db($con, name)
Serial number | Parameters and descriptions |
---|---|
1 | con(Required) This is an object representing the connection with the MySQL Server. |
2 | name(Required) This is a string value representing the database name. |
The PHP mysqli_select_db() function returns a boolean value, which istrue,otherwisefalse.
This function was originally in PHP version5introduced and can be used in all higher versions.
The following example demonstratesmysqli_select_db()Usage of the function (procedural style)-
<?php //Establish connection $con = mysqli_connect("localhost", "root", "password", "mydb"); //Select database mysqli_query($con, "CREATE DATABASE NewDatabase"); mysqli_select_db($con, "NewDatabase"); //Retrieve the current database name $res = mysqli_query($con, "SELECT DATABASE()"); while ($row = mysqli_fetch_row($res)) { print("Current database: ").$row[0]; } //Close connection mysqli_close($con); ?>
Output result
Current database: newdatabase
In object-oriented style, the syntax of this function is$con-> select_db();.Here is an example of this function in object-oriented style;
<?php //Establish connection $con = new mysqli("localhost", "root", "password", "mydb"); //Retrieve the current database name $res = $con-> query("SELECT DATABASE()"); while ($row = $res-> fetch_row()) { print("Initial database: ").$row[0]."\n"; } //Select database $con-> query("CREATE DATABASE NewDatabase"); $con-> select_db("NewDatabase"); //Retrieve the current database name $res = $con-> query("SELECT DATABASE()"); while ($row = $res-> fetch_row()) { print("Current database: ").$row[0]; } //Close connection $res = $con -> close(); ?>
Output result
Initial database: mydb Current database: newdatabase
In addition to specifying the database at the time of connection, you can also change the default connected database using this function later, as shown below:
<?php //Establish connection $con = mysqli_connect("localhost", "root", "password"); //Select database mysqli_select_db($con, "mydb"); print("Select database ...\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("Create table ...\n"); //Insert 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', 'Cape Town', 'South Africa')"); mysqli_query($con, "insert into my_team values(")3, 'Kumara', 'Sangakkara', 'Matale', 'Sri Lanka')"); mysqli_query($con, "insert into my_team values(")4, 'Virat', 'Kohli', 'Delhi', 'India')"); print("Insert record ...\n"); //Close connection mysqli_close($con); ?>
Output result
Select database ... Create table ... Insert record ...
Change the default database connection:
<?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 "Selected database"; }else{ echo "An error occurred"; } mysqli_close($connection_mysql); ?>
Output result
Selected database