mysqli_select_db() expects parameter 1 to be mysqli, string given
The error message you are encountering is a direct result of mixing up the order of arguments when calling mysqli_select_db(). The specific warning states that the function expects the first parameter to be an instance of the mysqli object, but your code is passing a string instead. This usually happens because the connection and the database selection logic are inverted or implemented incorrectly relative to the function's signature.
Looking at the problematic snippet from connection.php, the root cause lies in line 11:
$db_select = mysqli_select_db(DB_NAME, $connection);
In this implementation, you are passing $DB_NAME (a string) as the first argument and $connection (the result of mysqli_connect) as the second. The PHP documentation for mysqli_select_db() explicitly defines its signature as bool mysqli_select_db(mysqli $link, string $database_name). This means the function requires the active connection object first to know which context it should operate within, followed by the name of the database you wish to switch to.
When you pass a string where an object is expected, PHP throws a type mismatch warning. While PHP allows mysqli_select_db() to accept a resource handle as the second argument in older versions or specific configurations, relying on this behavior is fragile. In modern PHP versions (7.0+), strict typing enforces that the first argument must be the link identifier. If you attempt to pass a string database name as the first argument, the function cannot validate the connection context, leading to the failure you are seeing.
The correct implementation requires swapping the arguments so that $connection is passed first, followed by DB_NAME. Here is how the corrected code should look:
require "constants.php";
// 1. Create a database connection
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS);
// Check if the connection was successful before attempting to select a database
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
// 2. Select a database to use (Corrected argument order)
$db_select = mysqli_select_db($connection, DB_NAME);
// It is good practice to verify the selection result
if (!$db_select) {
// Handle the case where the database does not exist or is inaccessible
die("Database selection failed: " . mysqli_error($connection));
}
There are a few critical nuances here that often get overlooked. First, always check if $connection evaluates to false immediately after mysqli_connect(). Attempting to select a database on a failed connection will invariably result in an error, and wrapping this logic in a conditional block prevents obscure debugging issues later. Second, checking the return value of mysqli_select_db() is essential. The function returns true on success and false on failure. If your script proceeds without checking this return value, you might accidentally execute queries against the default database or fail silently depending on how subsequent SQL logic handles the missing context.
Another common pitfall involves how PHP handles the first argument in different contexts. While some older tutorials suggest passing the database name first to avoid storing a connection object variable, mysqli_select_db() does not work that way. It is designed to modify an existing link resource. If you are using prepared statements or managing multiple connections throughout your application lifecycle, maintaining the $connection object reference is mandatory.
If you are working within a larger framework like Laravel or building a custom ORM, ensure that your database configuration arrays reflect this order when initializing connections programmatically. Misordering these parameters often stems from confusing mysqli_select_db() with procedural MySQL functions like mysql_select_db(), which took the database name as the first argument. Since the procedural extension is deprecated and removed in PHP 7.0+, sticking to the object-oriented syntax is not just a best practice; it is a requirement for your code to run on modern servers.
For developers migrating legacy code, this specific error is a frequent stumbling block. The solution is straightforward: ensure the connection resource is the first argument. If you encounter issues where mysqli_connect() returns false but you don't see an immediate error, check your PHP error reporting settings (error_reporting and display_errors). Sometimes warnings are suppressed by default in production environments, hiding the type mismatch until a query fails catastrophically.
