Current Location: Home> Latest Articles> The correct way to combine connect() with mysqli_select_db()

The correct way to combine connect() with mysqli_select_db()

M66 2025-05-22

When using PHP to operate MySQL databases, the connect() function and mysqli_select_db() are often used in conjunction with the database server to enable connection with the database server and select specific databases. In modern PHP, we recommend using mysqli extension, which is safer, more powerful than traditional mysql extensions, and supports object-oriented programming.

This article will introduce how to use the mysqli_connect() function and the mysqli_select_db() function to combine the operation of the database, precautions, and how to improve the stability and maintainability of the code.

1. Basic usage

Let’s first look at the most basic way to write connections and database selection:

<code> $host = 'localhost'; $user = 'db_user'; $password = 'db_password'; $dbname = 'my_database';

// Create a connection
$conn = mysqli_connect($host, $user, $password);

if (!$conn) {
die('Connection failed: ' . mysqli_connect_error());
}

// Select database
if (!mysqli_select_db($conn, $dbname)) {
die('Database selection failed: ' . mysqli_error($conn));
}

echo "Database connection and selection succeeded!";
</code>

This code does two things:

  1. Use mysqli_connect() to connect to the database server;

  2. Use mysqli_select_db() to select a database.

2. Why not directly specify the database in mysqli_connect() ?

mysqli_connect() actually supports passing in database names directly, which means you can simplify the code:

<code> $conn = mysqli_connect($host, $user, $password, $dbname); </code>

This method is good enough in most cases, but manual call to mysqli_select_db() still has its application scenarios:

  • When you need to connect to the server for permissions or user information verification first, then select a different database;

  • When the database name is determined dynamically at runtime;

  • In some frameworks, it is necessary to establish a database connection pool first and then dynamically switch the database according to business needs.

Therefore, while the simplified version is more commonly used, it is equally important to understand the way to manually call mysqli_select_db() .

3. Error handling suggestions

Do not ignore error handling during development. Connection failure or database selection failure is a common problem. Be sure to check the return value of each step:

<code> if (!$conn) { error_log("Database connection failed: ". mysqli_connect_error()); exit("The system is busy, please try again later."); }

if (!mysqli_select_db($conn, $dbname)) {
error_log("Database selection failed:" . mysqli_error($conn));
exit("Database exception, please contact the administrator.");
}
</code>

Logging error information can help troubleshoot problems and improve user experience.

IV. Practical application examples

Suppose you are developing a site <code> https://www.m66.net/login.php </code>, and the user needs to verify its information after submitting the form. The code logic may be as follows:

<code> $conn = mysqli_connect('localhost', 'user', 'pass');

if (!$conn) {
die("Database connection failed!");
}

mysqli_select_db($conn, 'user_db');

$username = $_POST['username'];
$password = $_POST['password'];

$query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($conn, $query);

if (mysqli_num_rows($result) > 0) {
header("Location: https://www.m66.net/dashboard.php ");
} else {
echo "Error in username or password";
}
</code>

Note : For the sake of simplicity of the example above, no preprocessing statement is used. In actual production environment, mysqli_prepare() must be used to prevent SQL injection.

5. Summary

Using the combination of mysqli_connect() and mysqli_select_db() in PHP is a flexible database connection method, especially suitable for scenarios where databases need to be switched dynamically. Although most occasions you can specify a database name directly at the time of connection, understanding the independent use of these two can help write more robust code.

Always remember that a good error handling mechanism is a key step in developing high-quality PHP applications. Hopefully this article helps you use these two functions correctly and efficiently for database operations.