Decoding the mysqli_num_rows() Boolean Warning and Database Connection Errors
The error message Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given is one of the most common stumbling blocks for developers transitioning from procedural scripts to more robust architectures. It signals a fundamental misunderstanding of how the MySQLi extension handles queries in PHP. When you pass a boolean false returned by mysqli_query() directly into mysqli_num_rows(), the function crashes because it expects an object representing a result set, not a success/failure flag.
In the scenario described, the initial code snippet attempted to execute a query and immediately check the row count:
if(mysqli_num_rows($results) >= 1)
The variable $results was likely uninitialized or held a boolean value because the preceding mysqli_query() call failed silently or returned false without being checked. The error occurs specifically when $results contains false, indicating that the SQL statement could not be executed successfully.
Validating Query Results Immediately
The most critical rule in database programming with MySQLi is to check the return value of mysqli_query() before attempting to process the result set. When you run a query, if the syntax is incorrect, a table is missing, or permissions are denied, mysqli_query() returns false. If you proceed to call mysqli_num_rows() on that boolean, PHP throws the warning you encountered.
To fix this, you must inspect the return value immediately after execution:
$results = mysqli_query($link, $query);
if (!$results) {
die("Query failed: " . mysqli_error($link));
}
// Only safe to call num_rows here
echo mysqli_num_rows($results);
This pattern ensures that any SQL errors are caught and reported before you attempt to iterate over the data. If $results is false, calling mysqli_num_rows() is invalid, regardless of the error type (syntax error, missing table, or permission issue).
Addressing the "Table Doesn't Exist" Error
After implementing the fix above, the user encountered a new error: Table 'sookehhh_shopsy_db.sookehhh_shopsy_db' doesn't exist. This is not a username issue; it is a syntax error within the SQL statement itself.
The connection code provided was:
$link = mysqli_connect($host, $username, $password, $db_name) or die('Could not connect: ' . mysqli_connect_error());
Here, mysqli_connect() accepts four arguments in the procedural style: host, username, password, and database name. When you pass a database name as the fourth argument to mysqli_connect(), it automatically selects that database for subsequent operations. This sets the current database context correctly.
However, the SQL query defined in the variable was:
$query = "SELECT * FROM sookehhh_shopsy_db WHERE name LIKE '%" . mysqli_real_escape_string($link, $searchTerm) . "%'";
The table name in the query string is sookehhh_shopsy_db, which matches the database name. In MySQL, you cannot query a table that shares the exact same name as the database it resides in unless you fully qualify it or if they are different entities (which is rare in this context). The error message suggests the system interpreted the query as looking for a table named sookehhh_shopsy_db inside a database also named sookehhh_shopsy_db.
If the intention is to search within a specific table, say products, the query should be:
$query = "SELECT * FROM products WHERE name LIKE '%" . mysqli_real_escape_string($link, $searchTerm) . "%'";
If the user actually created a database named sookehhh_shopsy_db and expects to query a table with the same name (e.g., CREATE DATABASE sokehhhh_shopsy_db; CREATE TABLE sookehhh_shopsy_db (...)), this is valid SQL, but it often indicates a confusion between database names and table names during setup.
Security Considerations in Search Queries
The code snippet included mysqli_real_escape_string($link, $searchTerm). While this prevents SQL injection by escaping special characters in the user input, it is important to note that this function is deprecated in newer versions of PHP (8.1+) and will be removed in future releases.
For modern applications, the preferred approach is using Prepared Statements with bound parameters. This method separates the query structure from the data, offering superior protection against injection attacks:
$stmt = $link->prepare("SELECT * FROM sookehhh_shopsy_db WHERE name LIKE ?");
$searchTermEscaped = "%$searchTerm%";
$stmt->bind_param("s", $searchTermEscaped);
$stmt->execute();
$results = $stmt->get_result();
This approach is the industry standard and should be adopted to future-proof the application.
Moving Forward with Modern Stacks
For developers working on larger-scale projects or those looking to simplify database interactions, frameworks like Laravel abstract away these low-level details. Laravel's Eloquent ORM handles query building, binding parameters, and result fetching automatically, eliminating the need to manually manage mysqli_connect instances or worry about boolean returns from queries. For more information on how modern frameworks handle database abstraction, you can explore resources at https://laravelcompany.com.
Ultimately, resolving the mysqli_num_rows() error requires strict validation of query execution results. By checking for false returns and ensuring your SQL targets the correct table names, you can eliminate these runtime warnings and build more stable web applications.
