When connecting to MySQL databases using PHP, there are many ways to obtain query results. Mysql_fetch_array and mysql_fetch_row are two commonly used functions. They can all get a row of data from the query results, but the returned data structure and applicable scenarios are different. This article will explain in detail the differences between these two functions and their respective application scenarios.
mysql_fetch_array()
This function takes a row from the result set as an associative array, a numeric index array, or an array that has both. Its default behavior is to return an array containing numeric indexes and associated indexes.
Syntax example:
$row = mysql_fetch_array($result, MYSQL_BOTH);
The second parameter can be:
MYSQL_ASSOC : Return only associative array
MYSQL_NUM : Return only the numeric index array
MYSQL_BOTH (default): Returns both associative and numeric index arrays
mysql_fetch_row()
This function takes a row from the result set and returns it as a numeric index array. That is to say, the result can only be accessed through a numeric index and cannot be accessed by a field name.
Syntax example:
$row = mysql_fetch_row($result);
characteristic | mysql_fetch_array | mysql_fetch_row |
---|---|---|
Return value type | Arrays that return numeric index + associated index by default (optional) | Returns the numeric index array |
Access methods | Accessible field names or numeric indexes | Only accessed with digital indexes |
Memory consumption | Larger (because the return array contains two indexes) | Smaller |
flexibility | High, suitable for scenarios where numerical indexes and associated indexes are required at the same time | Low, only suitable for digital index access |
Suitable for scenarios where flexible access to fields is required, for example:
The data needs to be accessed through the field name, and the code is more readable
It is necessary to support both numeric index and field name index, which has better compatibility
The amount of data is read is small and the memory usage requirements are not high
Example:
$result = mysql_query("SELECT id, name FROM users");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "User ID: " . $row['id'] . ", Name: " . $row['name'] . "<br>";
}
Suitable for scenarios where memory requirements are strictly required and only accessed by digital indexes, such as:
Read big data in batches to save memory space
Only process field data in order, no need to access by field name
Simple data processing with high performance requirements
Example:
$result = mysql_query("SELECT id, name FROM users");
while ($row = mysql_fetch_row($result)) {
echo "User ID: " . $row[0] . ", Name: " . $row[1] . "<br>";
}
function | advantage | shortcoming |
---|---|---|
mysql_fetch_array | Flexible access, good code readability | Takes up more memory |
mysql_fetch_row | Low memory usage, better performance | Only accessed by digital indexes, poor code readability |
// use mysql_fetch_array Get the associative array
$result = mysql_query("SELECT id, username FROM users");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "ID: " . $row['id'] . ", Username: " . $row['username'] . "<br>";
}
// use mysql_fetch_row Get the numeric index array
$result = mysql_query("SELECT id, username FROM users");
while ($row = mysql_fetch_row($result)) {
echo "ID: " . $row[0] . ", Username: " . $row[1] . "<br>";
}