A Comprehensive Guide on mysqli_select_db Function in PHP
When it comes to interacting with MySQL databases in PHP, the mysqli extension provides a variety of functions to perform various operations. One such function
The mysqli_select_db() function changes the default (active) database for an existing MySQL connection. Once a database is selected, every subsequent query you run on that connection — SELECT, INSERT, UPDATE, and so on — targets that database unless you fully qualify a table name (e.g. other_db.users).
Most of the time you set the database when you open the connection with mysqli_connect(), and you never call mysqli_select_db() at all. It earns its place in one specific situation: when a single connection needs to work with more than one database and you want to switch the active one without reconnecting.
This guide explains the syntax, the return value, when you actually need the function, and the common mistakes to avoid.
Syntax
mysqli_select_db(mysqli $connection, string $database): bool| Parameter | Description |
|---|---|
$connection | The connection link returned by mysqli_connect(). Required. |
$database | The name of the database to set as the default for this connection. |
In object-oriented style the same operation is a method on the connection object:
$connection->select_db($database);Return value
mysqli_select_db() returns a boolean:
true— the database was selected successfully.false— the database does not exist, the user lacks permission, or the connection is invalid.
Because it returns a boolean, always check the result instead of assuming success. On failure, mysqli_error() gives you the reason.
A complete, runnable example
The snippet below connects without naming a database, then selects one explicitly and runs a query against it. Replace the credentials with your own to run it against a real server.
<?php
// 1. Connect WITHOUT choosing a database yet.
$connection = mysqli_connect('localhost', 'username', 'password');
if (!$connection) {
die('Connection failed: ' . mysqli_connect_error());
}
// 2. Pick the database this connection should use.
if (mysqli_select_db($connection, 'shop')) {
echo "Database 'shop' selected.\n";
} else {
die("Could not select database: " . mysqli_error($connection));
}
// 3. Every query now runs against 'shop'.
$result = mysqli_query($connection, 'SELECT name FROM products LIMIT 1');
mysqli_close($connection);When you actually need it
You usually pass the database name straight to mysqli_connect():
// The 4th argument selects the database immediately — no select_db() needed.
$connection = mysqli_connect('localhost', 'username', 'password', 'shop');Reach for mysqli_select_db() only when you need to switch databases on one open connection, for example a reporting script that copies rows between two databases on the same server:
<?php
$connection = mysqli_connect('localhost', 'username', 'password');
// Read from the live database.
mysqli_select_db($connection, 'live');
$rows = mysqli_query($connection, 'SELECT id, total FROM orders');
// Switch the same connection to the archive database and write there.
mysqli_select_db($connection, 'archive');
// ... insert $rows into archive.orders ...
mysqli_close($connection);Switching with mysqli_select_db() is cheaper than opening a second connection, which is why it exists.
Common mistakes
- Ignoring the return value. A failed selection leaves the previous database active (or none at all), so later queries silently hit the wrong tables. Always branch on the boolean.
- Wrong argument order. The connection comes first, the database name second:
mysqli_select_db($connection, 'shop'). Reversing them is a frequent bug. - Confusing it with
mysqli_connect().select_db()does not authenticate or open anything — it only changes the default database of a connection that already exists. - SQL injection from user input. A database name is an identifier and cannot be parameterized with a prepared statement. Never build it from raw user input; restrict it to a fixed allow-list of known names.
Related functions
mysqli_connect()— open a connection (and optionally select a database in one step).mysqli_query()— run SQL against the currently selected database.mysqli_fetch_assoc()— read result rows as associative arrays.- PHP MySQLi overview — the full mysqli extension at a glance.
Conclusion
mysqli_select_db() sets the default database for an existing MySQL connection. Prefer selecting the database directly in mysqli_connect(), and keep mysqli_select_db() for the case it was built for: switching between databases on a single connection. Whichever you use, always check the boolean return value so a failed selection never sends your queries to the wrong database.