This is a simple question for PHP users. The reason I couldn\'t get the the exact difference between mysql_fetch_array()
and mysql_fetch_row()
in P
As the manual says: mysql_fetch_array() can return an associative array depending on defaults and its second parameter (MYSQL_ASSOC
, MYSQL_NUM
, or MYSQL_BOTH
).
While mysql_fetch_row() always returns an indexed array.
Answer two from the first link you provided is correct. The comment said:
"Instead both returns all the rows from table. The difference between them is fetch_array return result in assoc array as well as numeric array and you can also specify which specific type of array you want by providing second parameter to the function while fetch_row return result in numeric array only."
... so using fetch_row you can't do something like
echo $result['name']; // can do this in fetch_array, not in fetch_row
echo $result[0]; // can do this in fetch_array and fetch_row
mysql_fetch_row returns an enumerated array, so the index are numbers. mysql_fetch_array returns an associative array (and by defaults, merge numbers for index), so, mysql_fetch_array returns, by default, all data duplicated in a single array, one set of data using number as index and another set, still in the same array, with associative index (text based indexes).
mysql_fetch_row Example:
Array(2)
0 => "foo"
1 => "bar"
mysql_fetch_array Example (default behavior):
Array(4)
0 => "foo"
1 => "bar"
"user" => "foo"
"name" => "bar"
Many of the php programming newbies get confused about mysql_fetch_array(), mysql_fetch_row(), mysql_fetch_assoc() and mysql_fetch_object() functions, but all of these functions performs a similar process.
Let us create a table “tb” for clear example with three fields “id”, “username” and “password”
Table: tb
Insert a new row into the table with values 1 for id, tobby for username and tobby78$2 for password
db.php
<?php
$query=mysql_connect("localhost","root","");
mysql_select_db("tobby",$query);
?>
mysql_fetch_row()
Fetch a result row as an numeric array
<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_row($query);
echo $row[0];
echo $row[1];
echo $row[2];
?>
</html>
Result
1 tobby tobby78$2
mysql_fetch_object()
Fetch a result row as an object
<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_object($query);
echo $row->id;
echo $row->username;
echo $row->password;
?>
</html>
Result
1 tobby tobby78$2
mysql_fetch_assoc()
Fetch a result row as an associative array
<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_assoc($query);
echo $row['id'];
echo $row['username'];
echo $row['password'];
?>
</html>
Result
1 tobby tobby78$2
mysql_fetch_array()
Fetch a result row as an associative array, a numeric array and also it fetches by both associative & numeric array.
<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_array($query);
echo $row['id'];
echo $row['username'];
echo $row['password'];
<span style="color: #993300;">/* here both associative array and numeric array will work. */</span>
echo $row[0];
echo $row[1];
echo $row[2];
?>
</html>
Result
1 tobby tobby78$2