mysqli_query() expects at least 2 parameters, 1 given in?
When you encounter the warning mysqli_query() expects at least 2 parameters, 1 given, the root cause is almost invariably a misunderstanding of the function's signature and how it interacts with string variables versus resource objects. In your specific case, the code passes a SQL query string as the first argument instead of an active database connection handle.
The mysqli_query() function requires two mandatory arguments:
- The connection object (e.g.,
$con). - The SQL query string (e.g.,
"SELECT * FROM users").
Your current implementation attempts to execute the query like this:
$search_query = mysqli_query($search_sql);
Here, $search_sql contains the string "Select * FROM Questions...". By passing a string into mysqli_query(), the function tries to treat that string as a connection resource. Since it is not a valid connection object, MySQLi returns false (or null depending on the specific version and error reporting settings), triggering the first warning.
Correcting the Function Call
The fix is straightforward: you must pass the $con variable created earlier in your script as the first argument. Here is the corrected syntax:
// Incorrect
$search_query = mysqli_query($search_sql);
// Correct
$search_query = mysqli_query($con, $search_sql);
By adding $con, you explicitly tell MySQLi which database connection to use for executing this specific query. Without this, the function has no context regarding which server or database schema you are targeting.
Handling Query Results and Null Returns
The subsequent warnings regarding mysqli_num_rows() are direct consequences of the first error. Because mysqli_query() failed due to the missing connection parameter, it returned null (or a false value that evaluates as null in this context). When your code immediately calls mysqli_num_rows($search_query), you are passing null where an active result set (mysqli_result) is expected.
// This triggers: "expects parameter 1 to be mysqli_result, null given"
if (mysqli_num_rows($search_query) != 0) { ... }
To handle this robustly, you should always check if the query succeeded before attempting to inspect its results. A common pattern is:
$search_query = mysqli_query($con, $search_sql);
if (!$search_query) {
die("Query failed: " . mysqli_error($con));
}
// Now it is safe to call num_rows
if (mysqli_num_rows($search_query) > 0) {
// Process results
}
Security and Modern Best Practices
While fixing the parameter order resolves the immediate crash, your current implementation contains significant security vulnerabilities that should be addressed immediately. You are currently using raw string interpolation with $_POST['search']:
$search_sql = "Select * FROM Questions WHERE username LIKE '%".$_POST['search']."%' ...";
This approach opens the door to SQL Injection. A malicious user could enter a string like ' OR '1'='1 to bypass your search logic entirely and potentially access sensitive data.
Instead of concatenating strings, you should use prepared statements with bound parameters. This separates the SQL structure from the data, ensuring that user input is always treated as text rather than executable code.
$search_query = "SELECT * FROM Questions WHERE username LIKE ? OR feedback LIKE ?";
// Prepare the statement
$stmt = mysqli_prepare($con, $search_query);
// Bind parameters: 's' stands for string
$search_term = '%' . $_POST['search'] . '%';
mysqli_stmt_bind_param("ss", $param1, $param2, $search_term, $search_term);
// Execute the query
mysqli_stmt_execute($stmt);
// Get the result
$search_result = mysqli_stmt_get_result($stmt);
Legacy Syntax Warnings
Your code also uses do ... while loops with mysqli_fetch_assoc. While this syntax works in older versions of MySQLi, it is generally discouraged in modern development. The standard approach involves a simple while loop that fetches rows one by one until the result set is exhausted:
while ($search_rs = mysqli_fetch_assoc($search_query)) {
echo $search_rs['name'];
}
This pattern is cleaner, easier to debug, and aligns with how modern frameworks like Laravel handle database iteration. If you are looking to migrate away from procedural MySQLi syntax entirely, consider using the Object-Oriented interface or switching to PDO/Eloquent within a Laravel project for better maintainability and security features.