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 PHP is that I had been working much with Java.
Before I post this question here, I got some answers from Google but I found they're somewhat confusing. Some of the links I found on the internet are as follows.
Answer 1
Answer 2
Answer 3
Answer 4
I couldn't get the exact idea from the above answers. So actually what is the exact difference between them?
Difference: mysql_fetch_assoc() will always assing a non-secuencial key (like "color" and not a number). mysql_fetch_array() will assing a number key if there are not "word" key (0 and not "color" if there are not "color") but, you can choose to assing of key with a parameter...
mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.
mysql_fetch_array is a PHP function that will allow you to access data stored in the result returned from the TRUE mysql_query if u want to know what is returned when you used the mysql_query function to query a Mysql database.
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
The documentation is pretty clear on this, have you looked at it ?
mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] )
Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows. The type of returned array depends on how result_type is defined. By using MYSQL_BOTH (default), you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), [by] using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).
mysql_fetch_row ( resource $result )
Returns an numerical array of strings that corresponds to the fetched row, or FALSE if there are no more rows.
mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.
In summary
mysql_fetch_array( $result, MYSQL_ASSOC )
= mysql_fetch_assoc( $result )
mysql_fetch_array( $result, MYSQL_NUM )
= mysql_fetch_row( $result )
And
mysql_fetch_array ( $result )
= mysql_fetch_assoc( $result )
+ mysql_fetch_row( $result )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With