Returns all fields of the current row as a numerically indexed array, where each value corresponds to a column. The array index starts from 0, representing the column index in the result set.
mysql_fetch_assoc(): Returns all fields of the current row as an associative array, where the keys are column names and the values are the corresponding column values. Unlike mysql_fetch_row(), mysql_fetch_assoc() uses column names instead of numeric indexes to identify fields.
Example of mysql_fetch_row():
<span><span><span class="hljs-variable">$result</span></span><span> = </span><span><span class="hljs-title function_ invoke__">mysql_query</span></span><span>(</span><span><span class="hljs-string">"SELECT id, name, age FROM users"</span></span><span>);
</span><span><span class="hljs-variable">$row</span></span><span> = </span><span><span class="hljs-title function_ invoke__">mysql_fetch_row</span></span><span>(</span><span><span class="hljs-variable">$result</span></span><span>);
<p></span>echo $row[0]; // Outputs id<br>
</span>echo $row[1]; // Outputs name<br>
</span>echo $row[2]; // Outputs age<br>
</span></span>
In this example, mysql_fetch_row() returns a numerically indexed array, where the indices correspond to column positions (0, 1, 2…). To access a column’s data, we use its index.
Example of mysql_fetch_assoc():
<span><span><span class="hljs-variable">$result</span></span><span> = </span><span><span class="hljs-title function_ invoke__">mysql_query</span></span><span>(</span><span><span class="hljs-string">"SELECT id, name, age FROM users"</span></span><span>);
</span><span><span class="hljs-variable">$row</span></span><span> = </span><span><span class="hljs-title function_ invoke__">mysql_fetch_assoc</span></span><span>(</span><span><span class="hljs-variable">$result</span></span><span>);
<p></span>echo $row['id']; // Outputs id<br>
</span>echo $row['name']; // Outputs name<br>
</span>echo $row['age']; // Outputs age<br>
</span></span>
Unlike mysql_fetch_row(), mysql_fetch_assoc() returns an associative array, with column names as keys. This allows accessing data by column name, making the code easier to read and maintain.
From a performance perspective, mysql_fetch_row() is slightly faster than mysql_fetch_assoc(), as it returns a numeric array. mysql_fetch_assoc() involves mapping column names to array keys, which adds minor overhead. Therefore, in high-performance scenarios, mysql_fetch_row() may be more suitable.
However, in most cases, the performance difference is negligible and unlikely to be noticeable in typical applications.
Although mysql_fetch_row() has a slight performance edge, mysql_fetch_assoc() offers better code readability. Using column names instead of numeric indexes prevents errors and improves maintainability.
For example, if the order of columns in the result changes, using mysql_fetch_assoc() is more robust because column names remain consistent. In contrast, mysql_fetch_row() may cause errors if it relies on numeric indexes.
Use mysql_fetch_row() when:
Performance is critical and the result set contains many columns, requiring minimal memory usage.
The number of columns is fixed and unlikely to change, and the developer understands the column indexes well.
Use mysql_fetch_assoc() when:
Code readability and maintainability are important, especially if column order may change.
Accessing data by column name is needed to avoid errors due to column order or position changes.
The result set has few columns, and performance differences are insignificant.
mysql_fetch_row() and mysql_fetch_assoc() both retrieve a row from a MySQL result set, but they differ in return format, performance, and readability. Choosing between them depends on the specific scenario:
For performance-oriented tasks with simple data structures, mysql_fetch_row() is suitable.
For better readability and maintainability, especially with multiple columns where names may change, mysql_fetch_assoc() is more convenient.
In modern PHP development, it is recommended to use mysqli or PDO instead of the mysql extension, as mysql is deprecated. Understanding the differences between these two functions helps us work more efficiently with databases.