mysql_num_rows() expects parameter 1 to be resource, boolean given in

mysql_num_rows() expects parameter 1 to be resource, boolean given in

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

mysql_num_rows() expects parameter 1 to be resource, boolean given in

When you encounter the warning mysql_num_rows() expects parameter 1 to be resource, boolean given, the immediate reaction is often to suspect a PHP bug or a corrupted database state. In reality, this error message points directly to a failure in your SQL query execution logic before it ever reaches the row counting function. The underlying issue isn't with mysql_num_rows() itself; it's that you are trying to pass a boolean value where a result resource is expected.

The standard PHP flow for retrieving data counts looks like this:

  1. Execute the query using mysql_query().
  2. Check if the execution was successful.
  3. Pass the successful resource object to mysql_num_rows().

In your specific case, the error occurs because step 1 failed silently or explicitly returned a boolean false due to a syntax error in the SQL string. When mysql_query() encounters invalid syntax, it does not return a result set; it returns false. Consequently, when you pass that false value to mysql_num_rows(), PHP throws the warning stating that it expects a resource but received a boolean.

Looking at the query provided:

$query = "SELECT * FROM dreams WHERE titolo,titch LIKE \"%$trimmed%\" ORDER BY id_dreams DESC";

There is a critical syntax error hidden within the WHERE clause: titolo,titch. In SQL, you cannot list two columns separated by a comma inside a single condition like this. The comma acts as a separator for multiple independent conditions or column lists (such as in a SELECT statement), but here it creates an invalid expression. MySQL interprets this incorrectly, leading to the syntax error message: You have an error in your SQL syntax... near 'titch LIKE "%tags%"'.

Because the query fails syntax validation, mysql_query() returns false. The subsequent call to mysql_num_rows(false) triggers the warning you are seeing.

Fixing the SQL Logic

To resolve this, you must first ensure the SQL statement is valid. If your intention is to search for a string that appears in either the titolo or titch columns, you need to use logical operators. The correct syntax requires grouping conditions with parentheses and using the logical OR operator:

$query = "SELECT * FROM dreams 
          WHERE (titolo LIKE '%".$trimmed."%' OR titch LIKE '%".$trimmed."%') 
          ORDER BY id_dreams DESC";

Once the SQL is corrected, mysql_query() will return a valid resource object (assuming no other errors exist), allowing mysql_num_rows() to function correctly. You should also ensure you are handling the execution failure explicitly:

$result = mysql_query($query) or die("Query failed: " . mysql_error());
$numrows = mysql_num_rows($result);

The Deprecation Warning

While fixing the immediate error is essential, it is important to note that the mysql_* extension has been deprecated since PHP 5.5.0 and removed in PHP 7.0.0. Relying on these functions introduces significant security risks and limits your ability to leverage modern features like prepared statements to prevent SQL injection.

If you are maintaining legacy code, fixing the syntax error is a quick patch. However, for any new development or refactoring, you should migrate to mysqli or PDO. These interfaces support prepared statements, which separate the query structure from user input, ensuring data integrity and security. For example, using mysqli with prepared statements would look like this:

$stmt = $mysqli->prepare("SELECT * FROM dreams WHERE (titolo LIKE ? OR titch LIKE ?) ORDER BY id_dreams DESC");
$searchTerm = "%$trimmed%";
$stmt->bind_param("ss", $searchTerm, $searchTerm);
$stmt->execute();
$result = $stmt->get_result();
$numrows = $result->num_rows; // No need for a function call here

This approach eliminates the risk of SQL injection and provides cleaner code. If you are looking to modernize your PHP stack further, resources like https://laravelcompany.com offer insights into building robust applications with frameworks that enforce these best practices by default.

Our network