What is the difference between mysql_fetch_object and mysql_fetch_array in PHP?
The main difference between mysql_fetch_object and mysql_fetch_array in PHP is the way they return data from a MySQL result set. mysql_fetch_object returns each row as an object with property names that correspond to the column names, while mysql_fetch_array returns each row as an array with both numeric and associative keys. If you want to access data using object properties, use mysql_fetch_object. If you prefer accessing data using array keys, use mysql_fetch_array.
// Using mysql_fetch_object
$result = mysql_query("SELECT * FROM table");
while ($row = mysql_fetch_object($result)) {
echo $row->column_name;
}
// Using mysql_fetch_array
$result = mysql_query("SELECT * FROM table");
while ($row = mysql_fetch_array($result)) {
echo $row['column_name'];
}
Related Questions
- What are the advantages and disadvantages of using CSS variables in PHP for dynamic styling?
- How can one ensure that the browser initiates a download when using Content-Disposition: attachment in PHP?
- What are the different template options available for PHP development, such as Smarty, phpLIB, and FastTemplate?