Warning: mysqli_select_db() expects exactly 2 parameters, 1 given

Warning: mysqli_select_db() expects exactly 2 parameters, 1 given

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

Warning: mysqli_select_db() expects exactly 2 parameters, 1 given

You are encountering this error because you have omitted the database name as an argument to the function call. The mysqli_select_db() function in PHP requires two specific arguments: the connection object and the database name. In your current implementation of Connects.php, you are only passing the database name string 'phpcadet' as the sole parameter.

The error message is explicit about what is missing. The function signature for mysqli_select_db() is defined as bool mysqli_select_db ( mysqli $link , string $database_name ). When you write mysqli_select_db('phpcadet'), PHP interprets this as a single argument being passed where two are expected. The first parameter expects a resource handle representing an active MySQL connection, not a string literal.

To resolve the issue, you must pass the $con variable that was returned by your mysqli_connect() call as the first argument. The corrected code should look like this:

<?php
$connect_error = 'Sorry, we\'re experiencing connection issues.';
$con = mysqli_connect('localhost', 'root', 'PwdSQL5');

// Pass both the connection resource and the database name
mysqli_select_db($con, 'phpcadet') or die($connect_error);
?>

It is important to understand why the connection object is required as the first parameter. The mysqli_select_db() function operates on a specific server connection context. Without passing the $con resource, PHP cannot determine which MySQL instance to send the command to. If you were using multiple connections in your application script and attempted to select a database without specifying the link, the function would not know which database to switch to for which connection handle.

Furthermore, relying on or die() immediately after selecting the database is generally considered a fragile pattern for production environments. While this approach works for simple scripts or tutorials where immediate feedback is desired, it halts execution entirely upon failure. A more robust strategy involves checking if the selection was successful before proceeding with any queries that depend on that schema being active.

Consider separating the connection and database selection logic into distinct steps to handle errors gracefully:

<?php
$host = 'localhost';
$user = 'root';
$pass = 'PwdSQL5';
$db_name = 'phpcadet';

$con = mysqli_connect($host, $user, $pass);

if (mysqli_connect_error()) {
    die('Sorry, we\'re experiencing connection issues.');
}

// Check if the database exists and select it safely
if (mysqli_select_db($con, $db_name)) {
    // Database selected successfully; proceed with queries
    echo 'Connected to ' . $db_name;
} else {
    // Handle the case where the database doesn't exist or selection failed
    die('Sorry, we cannot access the specified database.');
}
?>

This structure allows your application to distinguish between a connection failure and a database selection failure. It also prevents silent failures where queries run against an unintended default database (often mysql or _).

In modern PHP development using frameworks like Laravel, the underlying MySQLi driver is often abstracted away via PDO or specific Eloquent configurations. If you are transitioning from raw MySQLi to a framework environment, understanding these low-level requirements helps in debugging issues that might surface when interacting directly with the database layer without ORM assistance. While the official documentation for such transitions can sometimes be scattered across various resources like https://laravelcompany.com, the core principle remains consistent: always ensure the connection resource is explicitly passed to functions that manipulate the active database context.

Our network