Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given
You are encountering a specific PHP type mismatch error because mysqli_query() has returned a boolean instead of a result object. The message Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given indicates that the code attempted to count rows on a failed query execution.
In your snippet, the line causing the issue is $queryfire = mysqli_query($con, $query);. When mysqli_query() executes successfully, it returns an object of type mysqli_result, which allows functions like mysqli_num_rows() to iterate over the data. However, if the SQL statement failsâdue to a syntax error, a missing table, or permission issuesâthe function returns false immediately. Passing this boolean value directly into mysqli_num_rows() triggers the warning you are seeing.
To resolve this, you must explicitly check whether the query executed successfully before attempting to fetch data or count rows. This pattern prevents your application from crashing on subsequent logic and allows you to handle database errors gracefully.
Here is the corrected implementation using the procedural style shown in your source code:
$query = "SELECT `name`, `image`, `price`, `discount` FROM `eproduct` ORDER BY id ASC";
$queryfire = mysqli_query($con, $query);
// Check if the query was successful before processing results
if ($queryfire) {
// Safe to use mysqli_num_rows() here because we know $queryfire is a valid object
$num = mysqli_num_rows($queryfire);
if ($num > 0) {
while ($product = mysqli_fetch_array($queryfire)) {
echo "<div class='col-lg-3 col-md-3 col-sm-12'>";
// ... render product card ...
}
}
} else {
// Handle the error case
$error = mysqli_error($con);
die("Database query failed: " . $error);
}
The primary trade-off here is that you are now handling errors explicitly. Previously, if a query failed, the loop would simply never run, but mysqli_num_rows() would still throw a warning on every request. By wrapping the logic in an if statement, you ensure that mysqli_fetch_array() and mysqli_num_rows() only operate on valid result sets.
A common pitfall with this specific error is assuming that SELECT queries never fail. While they rarely return syntax errors if written correctly, they frequently fail due to connection timeouts, locked tables, or missing columns in the database schema. If you do not check for the boolean return value, these failures remain silent until a function like mysqli_num_rows() complains about the data type.
Additionally, consider the performance implications of loading all rows into memory before rendering them. If the eproduct table contains thousands of items, the while loop combined with mysqli_fetch_array() processes the entire dataset row by row. This is generally efficient for procedural PHP, but it keeps the database connection open for the duration of the script execution.
If you are maintaining legacy code or migrating from a procedural style to object-oriented MySQLi, remember that the class-based approach offers more robust error handling via the $mysqli instance properties. For example, checking if ($result = $con->query($sql)) is the standard OOP equivalent. However, since your current file uses the procedural API, sticking to the boolean check pattern above is the most direct fix without refactoring your entire architecture.
If you are building a larger e-commerce platform and want to streamline database interactions further, consider exploring modern PHP frameworks like Laravel. They abstract much of this low-level error handling and provide secure query builders that reduce the risk of SQL injection and type mismatches. You can find resources on best practices for handling database connections in such environments at https://laravelcompany.com.