In PHP using the old mysql extension, what are the differences between mysql_fetch_array(), mysql_fetch_object(), and mysql_fetch_row()?

Difficulty: Easy

Correct Answer: mysql_fetch_array() returns an array with both associative and numeric indexes, mysql_fetch_object() returns an object with properties named after the columns, and mysql_fetch_row() returns a numeric indexed array only

Explanation:


Introduction / Context:
Before mysqli and PDO became standard, many PHP applications used the mysql_* extension to work with MySQL databases. Even though this extension is deprecated, it still appears in older code and interview questions. Understanding the differences between mysql_fetch_array(), mysql_fetch_object(), and mysql_fetch_row() helps developers read and maintain legacy code and understand the general idea of how database fetch functions can return results in different formats.


Given Data / Assumptions:

  • A SELECT query has been executed with mysql_query() and returned a result resource.
  • We want to fetch rows one by one from this result set.
  • Each row contains one or more columns with names and positions.
  • Different fetch functions return rows in different structures.


Concept / Approach:
mysql_fetch_array() is a flexible function that returns a row as an array containing both numeric indexes (0, 1, 2, and so on) and associative indexes keyed by column names, unless a different fetch type is specified. mysql_fetch_object() returns the row as an object where each column is accessible as a property with the same name as the column. mysql_fetch_row() is a simpler function that returns a row as a numeric array only, where indexes correspond to column positions. Choosing between them depends on coding style and performance considerations.


Step-by-Step Solution:
Step 1: If you call mysql_fetch_array($result) without a second parameter, it returns an array where you can access a column named name as $row["name"] and also as $row[0] if it is the first column. This combined behaviour is flexible but may use more memory. Step 2: mysql_fetch_object($result) returns an object instead of an array. If there is a column named name, you access it as $row->name. This style is convenient for object oriented code and can make templates more readable. Step 3: mysql_fetch_row($result) returns a numeric array such as $row[0], $row[1], and so on. There are no associative keys; you must remember the order of columns in the query to interpret values correctly. Step 4: All three functions fetch the next row from the result set or return false when there are no more rows, and each call moves the internal pointer forward. Step 5: Developers may choose mysql_fetch_row() when they care about speed and minimal memory, mysql_fetch_array() when they want convenient access by name or position, and mysql_fetch_object() when they prefer property style access. Step 6: This behaviour matches the description in option a, which correctly distinguishes the return formats.


Verification / Alternative check:
You can verify these differences by executing a simple query such as SELECT id, name FROM a test table and then fetching one row with each function and printing the result with var_dump(). mysql_fetch_array() will show both numeric and associative keys, mysql_fetch_object() will show an object with id and name properties, and mysql_fetch_row() will show a simple numeric array. These experiments confirm that the three functions differ primarily in how they structure the returned row.


Why Other Options Are Wrong:
Option b is wrong because the functions are not identical and do not always return associative arrays. Option c is incorrect because it reverses roles and claims that one of the functions always throws an error, which is not correct in normal use. Option d is wrong because these functions are used only for fetching results from select queries; they do not apply to insert, update, or delete operations, which do not return result sets in the same way.


Common Pitfalls:
A common pitfall is to use mysql_fetch_array() without specifying a fetch type and then accidentally process the same data twice because both numeric and associative indexes exist. Another issue is failing to handle the case where the fetch function returns false, which can lead to warnings when code tries to access properties or indexes on a non array or non object. In modern PHP, developers should use mysqli or PDO instead, but understanding these legacy functions remains important when maintaining older systems.


Final Answer:
mysql_fetch_array() returns a row as an array with both associative and numeric indexes, mysql_fetch_object() returns a row as an object with properties named after the columns, and mysql_fetch_row() returns a row as a numeric indexed array only.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion