PHP :Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, string given

PHP :Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, string given

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

Stefan Bogdanescu

Founder & Senior Architect

PHP :Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, string given

This warning is a classic symptom of a variable shadowing issue in your PHP code. Specifically, you are passing a string to mysqli_num_rows() instead of the expected mysqli_result object.

In your provided code snippet, look closely at this line inside your HTML block:

<div class="row">
    <?php if(mysqli_num_rows($query)>0):?>

You are attempting to call mysqli_num_rows() on $query. However, earlier in your script, you defined $query as a string containing the SQL statement:

$query= "SELECT * FROM residential "; 
// ... concatenation logic ...
$result= mysqli_query($connect,$query);

At the point where the HTML block executes, $query still holds the raw SQL string (e.g., SELECT * FROM residential AND type='...'). The function mysqli_num_rows() expects an active result resource (the variable assigned to $result), not a string. Passing a string triggers the "expects parameter 1 to be mysqli_result" warning immediately.

The fix is straightforward: pass $result instead of $query.

<div class="row">
    <?php if(mysqli_num_rows($result) > 0):?>
        <?php while($row = mysqli_fetch_assoc($result)): ?>
            // Your rendering logic here
        <?php endwhile; ?>
    <?php else: ?>
        echo 'We found no record for your search criteria';
    <?php endif; ?>
</div>

However, relying solely on fixing this variable name does not address the deeper architectural issues present in the source material. The way the query is constructed and executed suggests several significant risks that should be addressed to ensure a robust application.

Query Construction and Security Risks

Your code builds the SQL statement dynamically by concatenating user input directly into the string:

if($type!=""){
    $query.=" AND  type='$type'";
}

This practice is highly susceptible to SQL injection attacks. If a user enters a value like ' OR '1'='1 into the $type field, your application will execute unintended database commands. Even though you check if variables are not empty before appending conditions, this does not protect against malicious payloads within those values.

You must use prepared statements. Prepared statements separate the SQL logic from the data, ensuring that user input is always treated as a string value rather than executable code. This also prevents syntax errors caused by special characters in your input fields like $unit_type or $min_price.

Here is how you should refactor the query preparation:

$query = "SELECT * FROM residential WHERE 1=1"; // Base query

if(!empty($type)){
    $query .= " AND type = ?";
}
if(!empty($unit_type)){
    $query .= " AND unit_type = ?";
}
// Continue for other conditions...

// Prepare the statement
$stmt = $connect->prepare($query);

// Bind parameters
$types = [];
if(!empty($type)) $types[] = $type;
if(!empty($unit_type)) $types[] = $unit_type;
// ... bind remaining variables ...

// Execute with bound parameters
$result = $stmt->execute();

Error Handling Logic

Your current error handling checks the query string itself:

if($query==FALSE){
    echo mysqli_error($connect);
    die;
}

This logic is flawed. The variable $query contains your SQL string, which will never be false. It will only be false if you explicitly assigned a false value to it. The actual result of the query execution is stored in $result. You should check $result for false and inspect $connect->error or $stmt->error (depending on whether you used an object-oriented connection) to retrieve the specific error message.

Fetching Data Correctly

In your loop, you are using mysqli_fetch_assoc($query). Again, this passes the string $query instead of the result resource. Consistency is key here; once you fix the initial check to use $result, the fetch operation must also use $result.

If you switch to prepared statements as recommended above, you will need to reset your variables after execution to ensure clean state management between requests. Always verify that $result holds a valid resource before attempting to iterate over it with while loops.

By correcting the variable references and adopting prepared statements, you eliminate the immediate warning and secure your application against common injection vulnerabilities.

Our network