PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP mysqli_select_db(): Select Database

The mysqli_select_db() function is used to change the default database for the connection in PHP.

Syntax:

The syntax of mysqli_select_db() is:

mysqli_select_db(mysqli $link, string $dbname): bool
  • $link: A mysqli link identifier returned by mysqli_connect().
  • $dbname: The name of the database.

Return Value:

This function returns TRUE on success or FALSE on failure.

Example:

Suppose you have a MySQL server with two databases: myDB and testDB. You initially connect to myDB, but then you want to change the default database to testDB. You can do this using mysqli_select_db():

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

// Change database to "testDB"
if (mysqli_select_db($conn, 'testDB')) {
  echo "Database is changed to testDB";
} else {
  echo "Could not change database";
}

$conn->close();
?>

In this example, we first create a connection to the myDB database using mysqli(). We then change the default database for the connection to testDB using mysqli_select_db(). If the database change is successful, we output a success message. Otherwise, we output an error message.

Note:

In many cases, it's easier and more straightforward to specify the database when you open the connection, using the fourth parameter of mysqli(). You should only need to use mysqli_select_db() if you need to switch to a different database after opening the connection.

  1. PHP mysqli_select_db() example code:

    • Example demonstrating the use of mysqli_select_db() to select a database.
    $mysqli = new mysqli("localhost", "username", "password");
    
    // Check connection
    if ($mysqli->connect_error) {
        die("Connection failed: " . $mysqli->connect_error);
    }
    
    // Select database
    $selectedDb = mysqli_select_db($mysqli, "database_name");
    
    if ($selectedDb) {
        echo "Database selected successfully";
    } else {
        echo "Error: " . mysqli_error($mysqli);
    }
    
    $mysqli->close();
    
  2. mysqli_select_db() vs mysqli_connect() in PHP:

    • Compare using mysqli_select_db() to switch databases after connection with directly specifying the database in mysqli_connect().
    // Using mysqli_select_db() after connecting
    $mysqli = new mysqli("localhost", "username", "password", "initial_database");
    mysqli_select_db($mysqli, "target_database");
    
    // Using mysqli_connect() with direct database specification
    $mysqli = new mysqli("localhost", "username", "password", "target_database");
    
  3. How to change the database connection in PHP:

    • Use mysqli_select_db() to change the database after establishing a connection.
    $mysqli = new mysqli("localhost", "username", "password", "initial_database");
    
    // Perform operations with the initial database
    
    // Change the database connection
    mysqli_select_db($mysqli, "new_database");
    
    // Perform operations with the new database
    
    $mysqli->close();
    
  4. mysqli_select_db() error handling in PHP:

    • Implement error handling to manage issues during the selection of a database.
    $mysqli = new mysqli("localhost", "username", "password");
    
    // Check connection
    if ($mysqli->connect_error) {
        die("Connection failed: " . $mysqli->connect_error);
    }
    
    // Select database with error handling
    $selectedDb = mysqli_select_db($mysqli, "database_name");
    
    if ($selectedDb) {
        echo "Database selected successfully";
    } else {
        echo "Error: " . mysqli_error($mysqli);
    }
    
    $mysqli->close();
    
  5. PHP switch database with mysqli_select_db():

    • Use mysqli_select_db() to dynamically switch between databases in PHP.
    $mysqli = new mysqli("localhost", "username", "password");
    
    // Check connection
    if ($mysqli->connect_error) {
        die("Connection failed: " . $mysqli->connect_error);
    }
    
    // Switch to the first database
    mysqli_select_db($mysqli, "database1");
    
    // Perform operations with database1
    
    // Switch to the second database
    mysqli_select_db($mysqli, "database2");
    
    // Perform operations with database2
    
    $mysqli->close();
    
  6. mysqli_select_db() and connection pooling in PHP:

    • Use mysqli_select_db() with connection pooling to efficiently manage database connections.
    $mysqli = new mysqli("p:localhost", "username", "password");
    
    // Check connection
    if ($mysqli->connect_error) {
        die("Connection failed: " . $mysqli->connect_error);
    }
    
    // Switch to the first database
    mysqli_select_db($mysqli, "database1");
    
    // Perform operations with database1
    
    // Switch to the second database
    mysqli_select_db($mysqli, "database2");
    
    // Perform operations with database2
    
    $mysqli->close();