PHP mysqli_select_db() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_select_db() chooses the default database for the open MySQL connection.

After a successful call, queries that omit a database prefix use this schema. You can also pass the database name when connecting. Switching databases mid-script is useful for admin tools, but most apps pick one database at connect time.

Syntax

Call mysqli_select_db() with the values you need. Start with this line.

PHP
$ok = mysqli_select_db($mysql, 'shop');
CallWhat it does
mysqli_select_db($link, 'app_db')Sets app_db as the default database on that link.
$mysqli->select_db('app_db')Object-oriented form.
mysqli_select_db($link, 'missing_db')Returns false when the schema does not exist or is denied.

You need an already open connection. Selecting a database is not the same as creating one. Prefer including the database in mysqli_connect() / real_connect() for simple apps.

Library declarations
PHP
mysqli_select_db(mysqli $mysql, string $database): bool

Parameters

ParameterDescription
mysqlRequired. Procedural style only: an open mysqli connection.
databaseRequired. Name of the database to make current.

Return Value

Return ValueDescription
trueThe default database was selected.
falseSelection failed.

PHP Examples

Example 1 — Select a Default Database

Demo of a successful select_db call with fixed output.

PHP
<?php
$db = 'demo_shop';
$ok = true; // stand-in for mysqli_select_db($link, $db)
echo $ok ? "using database {$db}" : 'select_db failed', PHP_EOL;

How It Works

  1. Open a connection first with connect or real_connect.
  2. Call mysqli_select_db($link, 'demo_shop').
  3. Later queries without db. table prefixes use demo_shop.
  4. Sample output when connected to a demo database confirms the switch succeeded.

Example 2 — Failure When the Name Is Wrong

Show how to react when select_db returns false.

PHP
<?php
$ok = false; // stand-in failure
$error = 'Unknown database 'nope'';
if (!$ok) {
    echo 'select_db failed: ', $error, PHP_EOL;
}

How It Works

  1. A missing or forbidden database yields false.
  2. Read mysqli_error() / errno() for the server message.
  3. Do not continue querying as if the schema were selected.
  4. Fix configuration rather than guessing names at runtime.

Example 3 — Connect With DB vs select_db

Two equivalent teaching patterns.

PHP
<?php
echo "A) mysqli_connect(host, user, pass, 'shop')", PHP_EOL;
echo "B) connect without db, then mysqli_select_db($link, 'shop')", PHP_EOL;

How It Works

  1. Pattern A sets the default database during connect.
  2. Pattern B switches later with select_db().
  3. Both end with the same default schema idea.
  4. Pick one style and keep it consistent in a project.

Frequently Asked Questions

It sets the default database on an open MySQLi connection.
No. You can pass the database name to mysqli_connect() / real_connect() instead.
true on success and false on failure.
Yes. Each successful call changes the default schema for later queries on that link.
No. Keep schema names in trusted config. Use prepared statements for row values, not for choosing databases.

Next: mysqli_set_charset()

mysqli_set_charset() sets the client character set for the connection, for example utf8mb4.

mysqli_set_charset() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful