Warning: Attempt to read property "num_rows" on bool in C:\xampp\htdocs\Nice Guys\index.php on line 59

Warning: Attempt to read property "num_rows" on bool in C:\xampp\htdocs\Nice Guys\index.php on line 59

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

Stefan Bogdanescu

Founder & Senior Architect

The Silent bool Error: Why Your SQL Query Returned False Instead of a Result Object

Encountering the error Warning: Attempt to read property "num_rows" on bool is one of the most confusing runtime exceptions you can face in PHP, particularly when working with MySQL. It suggests you are treating a boolean value as if it were an object. In your specific case, the stack trace points to line 59 in index.php, which corresponds to the check $searchResult->num_rows.

The root cause is rarely that your database connection failed silently; it is almost always that the query itself returned false instead of a mysqli_result object. When you call $db->query($searchSQL), the method returns an object on success and a boolean false on failure. If you do not explicitly check for errors before accessing properties, the subsequent line attempts to read num_rows on that false value, triggering the warning.

The Immediate Fix: Error Handling

The most direct way to resolve this is to ensure you are dealing with a valid result object before attempting to inspect it. You can wrap your query logic in a conditional check or use the mysqli_error() function (if using the procedural style) or check the return type directly.

Here is how you should structure the query block to prevent the crash:

$searchSQL = 'SELECT * FROM images WHERE MATCH (appname, firstname, lastname, image) AGAINST (?, ?) LIMIT ?, 10';

// It is highly recommended to use prepared statements here to prevent SQL injection,
// though the immediate error is about the return type.
$stmt = $db->prepare($searchSQL);

if (!$stmt) {
    die('Prepare failed: ' . $db->error);
}

// Bind parameters and execute
$stmt->bind_param("ssii", $search, $offset, 10, 0); // Adjust types based on your variables
$stmt->execute();

$searchResult = $stmt->get_result();

if (!$searchResult) {
    die('Query failed: ' . $db->error);
}

if ($searchResult->num_rows > 0) {
    while ($row = $searchResult->fetch_assoc()) {
        $output[] = [
            "image" => $row["image"], 
            "firstname" => $row["firstname"], 
            "lastname" => $row["lastname"], 
            "appname" => $row["appname"]
        ];
    }
}

$stmt->close();
$db->close();

The Real Issue: SQL Syntax vs. Return Types

While adding the check solves the immediate crash, it does not explain why the query returned false in the first place. In your original snippet, you are using full-text search with the MATCH ... AGAINST syntax. This is a powerful feature, but it comes with strict requirements:

  1. FULLTEXT Index Requirement: The columns listed inside the MATCH clause (appname, firstname, lastname, image) must have a FULLTEXT index defined on them in your database schema. If you run this query on a table where these columns are standard indexes or no indexes, MySQL will ignore the full-text search logic and potentially throw a syntax error or return an empty set depending on the configuration.
  2. Syntax Errors: Ensure that there are no stray characters inside the string interpolation. In your code, $search is directly concatenated into the SQL string: 'SELECT * ... AGAINST ("' . $search . '")'. If $search contains a quote character ('), it will break the SQL syntax immediately, causing query() to fail and return false.

Best Practice: Prepared Statements for Full-Text Search

Using prepared statements with full-text search can sometimes be tricky because the keyword must be escaped differently than standard parameters. However, checking the return value is non-negotiable. By verifying that $searchResult is an object before accessing num_rows, you isolate the failure to the database engine rather than your application logic.

If the query fails due to missing indexes, you will see a specific error message from MySQL when you add the error check. If it fails due to bad input in $search, that will also become visible once you stop the silent execution flow.

For production environments involving search functionality, consider leveraging dedicated search engines like Elasticsearch or Meilisearch if your requirements outgrow native SQL full-text capabilities. However, for standard CRUD applications with simple search needs, ensuring your schema supports MATCH ... AGAINST and validating query returns are the critical steps to stability.

Our network