When should mysql_select_db() be used in a PHP script and how does it impact database interactions?

The mysql_select_db() function should be used in a PHP script when you want to switch to a specific database within your MySQL server before executing queries. This function impacts database interactions by setting the current active database for subsequent queries, allowing you to work with tables and data from that specific database.

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

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

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Select the database
mysqli_select_db($conn, $dbname);

// Now you can start executing queries on the selected database
?>