Skip to content
PHP Logo

In what scenarios would it be advisable to use mysql_fetch_array or mysql_fetch_object instead of mysql_result in PHP?

When fetching data from a MySQL database in PHP, it is advisable to use mysql_fetch_array or mysql_fetch_object instead of mysql_result when you need to retrieve multiple rows of data or if you need to access multiple columns from a single row. mysql_result is more suitable for retrieving a single value from a single row.

php
// Using mysql_fetch_array to retrieve multiple rows of data
$result = mysql_query("SELECT * FROM table");
while ($row = mysql_fetch_array($result)) {
    // Access data using column names or indexes
    echo $row['column_name'];
}

// Using mysql_fetch_object to retrieve multiple rows of data
$result = mysql_query("SELECT * FROM table");
while ($row = mysql_fetch_object($result)) {
    // Access data using object properties
    echo $row->column_name;
}