What is the difference between mysql_fetch_array and mysql_fetch_assoc in PHP?
The main difference between mysql_fetch_array and mysql_fetch_assoc in PHP is the way they return data from a MySQL database query. mysql_fetch_array returns data as both a numerical and associative array, while mysql_fetch_assoc only returns data as an associative array. If you only need to access data by column names, mysql_fetch_assoc is more efficient to use.
// Using mysql_fetch_assoc to fetch data from a MySQL database query
$result = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_assoc($result)) {
echo $row['column_name'];
}
Related Questions
- What are the advantages and disadvantages of storing images in a MySQL database vs. storing them in the file system and storing only the file path in the database?
- Are there any best practices for managing cookies in PHP to avoid path-related issues?
- Is it recommended to store form data in a database or as a file when working with PHP?