while ($row = mysql_fetch_array($result)) - how many loops are being performed?

while ($row = mysql_fetch_array($result)) - how many loops are being performed?

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

Stefan Bogdanescu

Founder & Senior Architect

The mysql_fetch_array Misunderstanding: One Call, One Row

There is a persistent myth in PHP development circles that wrapping a database cursor in a loop creates "nested" loops or multiplies the iteration count based on the number of columns returned. This confusion often stems from conflating how you iterate over rows with how you iterate over data within those rows. Let's dissect exactly what happens under the hood when using mysql_fetch_array().

The Cursor Pattern: Iteration by Row

When you execute a query like $result = mysql_query($query), the database server returns a result set containing multiple rows (in this case, where id > 100). The variable $result acts as a cursor pointing to the first row of that dataset.

$query = "SELECT col1,col2,col3 FROM table WHERE id > 100";
$result = mysql_query($query);

The function mysql_fetch_array() is designed specifically to advance this cursor forward by exactly one step each time it is invoked. It returns the row currently pointed to and moves the internal pointer to the next row. If you call it again, it fetches the second row.

Consequently, a standard while loop:

while ($row = mysql_fetch_array($result)) {
   // Process $row here
}

performs exactly one iteration per row in the result set. If your query returns 100 rows, the code inside that block executes 100 times. There is no hidden multiplication happening here; the loop structure and the fetch function are synchronized to process the dataset linearly. This is known as a cursor-based traversal pattern, which was the standard for procedural PHP before PDO and mysqli became prevalent.

The foreach Trap: Iteration by Column

The confusion often arises when developers attempt to combine row fetching with column iteration using foreach. Consider this snippet:

$row = mysql_fetch_array($result);
foreach($row as $r) {
   ...
}

In this specific context, you are indeed executing two distinct loops sequentially. However, the relationship between them is crucial to understanding the performance and logic implications.

The first line, $row = mysql_fetch_array($result), performs a single fetch operation. It retrieves one complete row (e.g., 3 columns) and returns it as an associative or indexed array. This costs exactly one unit of "fetch iteration."

Immediately following that, the foreach loop iterates over the elements inside that single fetched row. If you selected three columns (col1, col2, col3), the foreach loop runs three times.

If you nest this structure inside a while loop to process all rows:

while ($row = mysql_fetch_array($result)) {
    foreach($row as $r) {
        // Logic per column
    }
}

You are not creating "2 loops iterated x times" in the sense of doubling the database fetch cost. You are iterating x times for the rows, and within each row, you are iterating y times for the columns. The total number of inner loop executions is rows × columns. This is functionally necessary if your logic depends on processing every column individually for every record, but it does not imply that mysql_fetch_array returned multiple rows in a single call.

Why This Matters

Understanding this distinction prevents logical errors when migrating legacy code. The mysql_* extension is deprecated as of PHP 5.5 and removed entirely in PHP 7.0. Attempting to use these functions in modern environments will result in fatal errors.

If you are maintaining legacy systems or learning the patterns that preceded modern extensions, remember: mysql_fetch_array() fetches one row. To process multiple rows, you must call it repeatedly within a loop. To process multiple columns, you must iterate over the resulting array. The loops serve different dimensions of the data structure—one traversing the vertical axis (rows) and the other the horizontal axis (columns).

For modern applications, the procedural mysql_* functions should be replaced with PDO or MySQLi, which offer prepared statements and better error handling. In those contexts, the behavior remains consistent: fetch methods return one row per call, and iteration logic must explicitly define how to traverse both rows and columns to avoid unintended performance bottlenecks or infinite loops caused by cursor exhaustion.

Our network