Difference between mysql_fetch_array and mysql_fetch_row?

Difference between mysql_fetch_array and mysql_fetch_row?

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

Stefan Bogdanescu

Founder & Senior Architect

Difference between mysql_fetch_array and mysql_fetch_row

In the landscape of legacy PHP development, distinguishing between mysql_fetch_array() and mysql_fetch_row() is a common hurdle, especially for engineers coming from strongly typed languages like Java. While modern stacks have largely migrated away from the deprecated MySQL extension in favor of PDO or MySQLi, understanding these functions remains essential for maintaining older codebases or preparing for certification exams that still reference them.

The core distinction lies entirely in the data structure returned by each function and how that data is accessed within your script. Both functions iterate over a result set, but they package the rows differently.

Data Structure Differences

When you call mysql_fetch_array(), the returned value is an associative array. This means the keys correspond to the column names defined in your SQL query. If your query selects id and name, accessing that row looks like this:

$row = mysql_fetch_array($result);
echo $row['id'];
echo $row['name'];

Conversely, mysql_fetch_row() returns a numeric array. The keys are sequential integers starting from 0, regardless of the column names. Accessing the same data requires index-based syntax:

$row = mysql_fetch_row($result);
echo $row[0]; // Corresponds to 'id'
echo $row[1]; // Corresponds to 'name'

This structural difference dictates how you interact with the data downstream. Associative arrays allow for semantic access, making the code self-documenting. Numeric arrays require you to know the column order, which can lead to brittle logic if a schema change occurs.

Memory and Performance Considerations

Beyond syntax, there is a tangible memory overhead involved in mysql_fetch_array(). By default, this function returns an array containing both numeric and associative indexes (e.g., $row[0] and $row['id']). This duplication consumes more memory per row compared to mysql_fetch_row(), which only stores the numeric index.

If you are processing a massive result set where memory footprint is a critical constraint, mysql_fetch_row() offers a slight efficiency gain. However, in most modern application contexts involving standard RAM sizes, this difference is negligible compared to the overhead of the database connection or network latency. The primary trade-off here is readability versus minimal resource savings.

Avoiding Common Pitfalls

A frequent mistake when working with mysql_fetch_array() involves mixing access styles unintentionally. If you rely on column names like $row['username'] but accidentally pass a boolean flag (often set to MYSQL_ASSOC or MYSQL_NUM) to the function, you can inadvertently break your code.

// Ambiguous: Works if keys exist, fails silently if not
echo $row['name']; 

// Explicit and safer
echo $row['name'] ?? null; 

Furthermore, because mysql_fetch_row() relies on column order, any change in the SQL query—such as adding a new column to the select list or reordering fields—will immediately break the numeric indexing logic. This makes mysql_fetch_row significantly harder to refactor than its associative counterpart.

The Modern Context

It is worth noting that the mysql_* functions are deprecated and removed in PHP 7.0+. For any new development, you should use PDO or MySQLi. In PDO, for instance, fetching a row as an associative array is the default behavior when using fetch() without arguments, but you can explicitly request numeric arrays using PDO::FETCH_NUM. This provides the same flexibility and control that mysql_fetch_row offered, but with modern security features like prepared statements to prevent SQL injection.

The choice between these legacy functions ultimately comes down to whether you prioritize semantic clarity or a fraction of memory savings. In almost all scenarios, the associative array returned by mysql_fetch_array() is the preferred approach due to its resilience against schema changes and improved code readability.

Our network