Warning: mysqli_query() expects at least 2 parameters, 1 given. What?
You are looking at a PHP error log that reads Warning: mysqli_query() expects at least 2 parameters, 1 given. At first glance, it looks like a simple syntax mistake. You might assume you just forgot to pass a variable or a string to the function. However, in this specific scenario, the root cause is actually how the database connection is established versus how the query is executed.
The error originates from a page trying to select two names from a database. The code attempts to connect using mysqli_connect and then immediately runs mysqli_query. While the logic seems sound on paper, the implementation of the connection string is where the failure occurs. In the provided snippet, the mysqli_connect function is called with four arguments: localhost, the username, the password, and the database name.
This specific invocation style has changed behavior significantly between PHP versions. In older versions of PHP (prior to 5.3), mysqli_connect would return a resource identifier on success if no error occurred. However, in modern PHP environments, which are likely running your server today, the function returns true or false instead of a resource. This change is subtle but critical for how subsequent functions handle the result.
The core issue here is that mysqli_query requires a valid connection resource as its first argument. When you call mysqli_connect without checking the return value or assigning it correctly, the variable holding the connection might not be what you think it is. In many cases, developers mistakenly believe the connection was successful simply because the script didn't crash immediately after the connect line. If the connection failed silently or if the variable wasn't assigned properly due to version-specific return type changes, mysqli_query receives a non-resource (like an integer or boolean) instead of a resource object. Since the function signature strictly expects a resource, PHP throws the "expects at least 2 parameters" warning because the first parameter is technically present but invalid for the operation you are attempting.
To resolve this, you must ensure that the connection variable holds a valid resource before passing it to mysqli_query. The robust pattern involves checking the return value of mysqli_connect or using mysqli_init followed by mysqli_realconnect. A corrected approach looks like this:
$host = 'localhost';
$username = 'tdoylex1_dork';
$password = 'dorkk';
$database = 'tdoylex1_dork';
$conn = mysqli_connect($host, $username, $password, $database);
if (!$conn) {
die("Connect Error: " . mysqli_connect_error());
}
// Now safe to use $conn in mysqli_query
$name1 = mysqli_query($conn, "SELECT name1 FROM users ORDER BY RAND() LIMIT 1");
By explicitly storing the result of mysqli_connect in a variable and verifying it, you guarantee that mysqli_query receives the correct resource type. This practice also makes debugging significantly easier if the database credentials are incorrect, as the error message will now point to the connection failure rather than the query execution.
Furthermore, notice how the code separates the connection logic from the query logic. Keeping them distinct prevents scope issues where a connection established in one context might not be accessible in another. If you are migrating this application to a modern PHP framework like Laravel, the underlying mechanism changes entirely using PDO or the Eloquent ORM, which handles these resource management details internally, abstracting away the low-level connection checks. However, if you must stick with procedural MySQLi, always remember that the first argument of mysqli_query is not optional; it is the lifeline to your database engine.
