What is difference between mysql_fetch_array(),mysql_fetch_row() and mysql_fetch_object()please insert with example

Questions by mrbaliram   answers by mrbaliram

Showing Answers 1 - 29 of 29 Answers

Geetan

  • Jun 26th, 2006
 

what is difference between mysql_fetch_array(),mys...

mysql_fetch_array():: fetches a result row as a associated array, numeric array

mysql_fetch_object: Fetaches a result row as object.

mysql_fetch_row::fetches a result row as array

mahalingam

  • Jul 30th, 2006
 

mysql_fetch_object -> Fetch the first single matching record of results.

mysql_fetch_array -> Fetch the all matching records of results.

  Was this answer useful?  Yes

Deepak Radhakrishnan

  • Feb 14th, 2007
 

mysql_fetch_row ? Get a result row as an enumerated arrayEach column has to be accessed as $row[0], $row[1],etcmysql_fetch_array ? Fetch a result row as an associative array, a numeric array, or both$row[0] or $row['user_id']both field name or enumerated array value can be usedmysql_fetch_object ? Fetch a result row as an object$row->user_id, $row->pass, etc

  Was this answer useful?  Yes

atul kumar pal

  • Jun 4th, 2007
 

mysql_fetch_row ::Return row as aan enumrated array and each line contain a unique ID .example.
$result=mysql_query($query);
while($row=mysql_fetch_row($result))
{
print "$row[0]";
print "$row[1]";
print "$row[2]";
}

mysql_fetch_array ::Return row as anassociative or an enumrated array or both which is default .you can refer to outputs as databases fieldname rather then number .example.
$result=mysql_query($query);
while($row=mysql_fetch_array($result))
{
print "$row['name']";
print "$row['address']";
print "$row['city']";
}

mysql_fetch_object  :: it return as an object on the place of array.

$result=mysql_query($query);
while($row=mysql_fetch_object($result))
{
print "$row->name";
print "$row->address";
print "$row->city";
}

shadab

  • Aug 5th, 2007
 


Let's take one example :
one table says
create table  student
(id  int(5),
name varchar(50));

mysql_fetch_array:

--> it returns array as index as table field name as well as it's index number
from table student you fetch record like this:
while($res=mysql_fetch_array($rs)){

    echo  $res['id'] ;// print id value
        or
    echo $res[0];// same as above statement
 
   

}

mysql_fetch_row:
--> it returns array as  it's index number
from table student you fetch record like this:
while($res=mysql_fetch_array($rs)){

 
    echo $res[0];// it can fetch value by index value not table field name
 
   

}



mysql_fetch_object
--> it returns array as  it's as object as table field name
from table student you fetch record like this:
while($res=mysql_fetch_array($rs)){

 
    echo $res->id;//  it can fetch value by object value not table field name or it's                                     //index
 
   

}

  Was this answer useful?  Yes

I will try to make you understand following  points.

 mysql_fetch_row: Returns row as an enumerated array
 mysql_fetch_object: Returns row as an object
 mysql_fetch_array: Returns row as an associative array

This  is the table details
------------------------------------------------------
ID Name       City
---------------------------------------------
1 belal Chatar
2 Sharma Ranchi
3 chand Bachra
------------------------------------------------------
$res="select * from details";              
$row=mysql_fetch_row($res) // this return enumerated array having value

row{
1
belal
chatra
}
so we can print $row[0],$row[1],$row[2];

in case of mysql_fetch_array() . It return accociative array.
like.
row['id']=1
row['name']="belal";
row['city']="Chatar";

Bela Ansari



  Was this answer useful?  Yes

mysql_fetch_array returns the array and you can only access the database fields as array but not by their names, but mysql_fetch_objewct returns the object and you can access the fields by using their names.

  Was this answer useful?  Yes

abc

  • Sep 12th, 2011
 

mysql_fetch_array():: fetches a result row as a associated array, numeric array

mysql_fetch_object: Fetaches a result row as object.

mysql_fetch_row::fetches a result row as array

  Was this answer useful?  Yes

Guru

  • Jun 26th, 2014
 

mysql_fetch_array(): return values as both an associative and numeric array
mysql_fetch_assoc(): return values as an associative array
mysql_fetch_row(): return values as an numeric array

  Was this answer useful?  Yes

SUBHASISH MANDAL

  • Jun 29th, 2016
 

Sir, Actually mysqli_fetch_array() and row() in both cases I can use echo $rwo[0] or $row[name] to retrieve data from database, thats why I could not get exactly difference between these.

  Was this answer useful?  Yes

archit attrey

  • Dec 27th, 2017
 

when we use mysql_fetch_array so in this condition we should pass value through an index or in the associative array through name instead of index. mysql_fetch_row only for a particular row. and mysql_fetch_object means we have to pass a value through object form like ($row = mysql_fetch_array())
$row[0] or $row[id]
if we use object than ($row = mysql_fetch_object())
$row -> id

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions