What is the difference between mysql_fetch_row() and mysql_fetch_array() in PHP when fetching data from a database?
The main difference between mysql_fetch_row() and mysql_fetch_array() in PHP is that mysql_fetch_row() returns a numerical array of the fetched row, while mysql_fetch_array() returns both a numerical and an associative array. If you only need the numerical array, mysql_fetch_row() is more efficient. However, if you need both numerical and associative arrays, mysql_fetch_array() is the better choice.
// Using mysql_fetch_row() to fetch data from a database
$result = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_row($result)) {
print_r($row);
}
// Using mysql_fetch_array() to fetch data from a database
$result = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_array($result)) {
print_r($row);
}
Related Questions
- What are the potential pitfalls of using the fetch_assoc function in PHP scripts, and how can it impact the encoding and display of special characters like umlauts?
- What is the correct way to use foreach loops in PHP when iterating through arrays?
- How can PHP developers modify anonweb to work with a proxy and implement a white-list feature effectively?