mysqli_query, mysqli_fetch_array and while loop

mysqli_query, mysqli_fetch_array and while loop

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

Stefan Bogdanescu

Founder & Senior Architect

The mysqli_query Return Value Trap

The error you are encountering, Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given, stems from a fundamental misunderstanding of how mysqli_query behaves in different contexts. Specifically, this function does not always return a result set resource. It returns a boolean false whenever the query fails or, critically, when the query does not produce a result set.

In your specific case, the SQL syntax error is the root cause. Look closely at this line:

$sqlCommand = "SELECT * property FROM portal";

There is a missing space between * and property. You have written SELECT * property, but the correct syntax requires a space before the table name: SELECT * FROM portal_property (assuming your table is named property). However, even if we fix that typo, there is a deeper logical issue. If you execute a query against an invalid table name or if the database connection $myConnection is not established correctly in connect_to_mysql.php, mysqli_query will return false.

When mysqli_query returns false, passing it directly to mysqli_fetch_array() triggers the warning because mysqli_fetch_array expects a resource (the result set), not a boolean. The function cannot iterate over nothing. To write robust PHP code, you must always validate the return value of mysqli_query before attempting to fetch rows.

Validating Query Results

Before entering your while loop, you need to check if the query was successful. This is standard practice in procedural MySQLi. If the query fails, you should handle the error immediately rather than letting it bubble up or causing confusing warnings later.

Here is how you can refactor the execution block:

$query = mysqli_query($myConnection, $sqlCommand);

// Check if the query was successful
if (!$query) {
    // Debugging: Log the specific error message to help identify syntax issues
    die('Query failed: ' . mysqli_error($myConnection)); 
}

// Now it is safe to fetch rows
while ($row = mysqli_fetch_array($query)) {
    // ... your logic here
}

By wrapping the fetch logic in a conditional check, you prevent the fatal error. If $query is false, the loop is skipped entirely, and you can address the underlying issue (like the SQL syntax error mentioned above) immediately.

Avoiding String Concatenation for HTML

While your immediate goal is to fix the query execution, there is a significant architectural pitfall in how you are building the output. You are using string concatenation (.) inside the while loop to build a massive HTML table string: $Displayproperty .= ....

This approach is inefficient and difficult to maintain. Every time you iterate through the database rows, PHP has to perform string parsing operations, which can degrade performance as the dataset grows. Furthermore, if the data in your portal database contains special characters like quotes or apostrophes, simply concatenating them into HTML without sanitization opens your application to Cross-Site Scripting (XSS) vulnerabilities.

Instead of accumulating a huge string in memory, render the HTML directly inside the loop. This keeps the logic clean and ensures that output buffering handles the streaming correctly.

echo '<table width="500" border="0" cellspacing="0" cellpadding="1">';

while ($row = mysqli_fetch_array($query)) {
    $id = $row["pid"];
    $title = $row["ptitle"];
    $area = $row["parea"];
    $city = $row["pcity"];
    
    // Echo the row immediately
    echo '<tr>';
    echo '<td>' . htmlspecialchars($id) . '</td>';
    echo '<td>' . htmlspecialchars($title) . '</td>';
    echo '<td>' . htmlspecialchars($area) . '</td>';
    echo '<td>' . htmlspecialchars($city) . '</td>';
    echo '<td><a href="forms.php?pid=' . $id . '">Upload images</a></td>';
    echo '</tr>';
}

echo '</table>';

Modern Alternatives and Migration Paths

If you are new to PHP, it is worth noting that the procedural MySQLi extension (mysqli_query, mysqli_fetch_array) is legacy technology. While it works, Laravel has adopted a different approach using PDO or its own query builder, which offers better security and syntax sugar.

The official Laravel documentation provides extensive guides on how to migrate from raw SQL to Eloquent ORM or use the Query Builder for cleaner data access patterns. For instance, instead of writing SELECT * FROM portal_property, you would interact with a model that handles connection pooling, binding, and prepared statements automatically. If you are planning to expand beyond simple localhost testing into a production environment, familiarizing yourself with these modern standards will save you from many common pitfalls associated with procedural MySQLi.

Our network