Skip to content
PHP Logo

In what situations should mysql_fetch_array be preferred over mysql_result in PHP scripts, and how can this improve code readability and performance?

mysql_fetch_array should be preferred over mysql_result when you need to fetch multiple columns from a MySQL result set. Using mysql_fetch_array allows you to access the data by column name or index, making your code more readable and maintainable. Additionally, mysql_fetch_array can be more efficient in terms of performance compared to mysql_result when fetching multiple columns.

php
// Using mysql_fetch_array to fetch multiple columns from a MySQL result set
$query = "SELECT column1, column2 FROM table";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)){
    echo $row['column1'] . " - " . $row['column2'] . "<br>";
}