what is difference between mysql_fetch_array(), mysql_fetch_row(), mysql_fetch_assoc() and mysql_fetch_object() in php

  • Share
  • CevherShare
  • Share

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”

READ ALSO: 5 PHP Functions to Fetch Data from Database

Table:  tb

Insert a new row into the table with values 1 for id, tobby for username and tobby78$2 for password

 

db.php

1
2
3
4
<?php
$query=mysql_connect("localhost","root","");
mysql_select_db("tobby",$query);
?>

 

mysql_fetch_row()

Fetch a result row as an numeric array

1
2
3
4
5
6
7
8
9
10
<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

1
2
3
4
5
6
7
8
9
10
<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

1
2
3
4
5
6
7
8
9
10
<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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<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

 

 

prasad

About prasad

Prasad K has written 111 post in this blog.

13 Responses to what is difference between mysql_fetch_array(), mysql_fetch_row(), mysql_fetch_assoc() and mysql_fetch_object() in php

  1. Ankit Saxena says:

    Thank bady this information very useful for me main interview last qus is how many types of faching data in mysql and your this blog very helpful for me then i can know diff b/w mysql_fetch_arry and mysql_fetch_assoc

  2. Very nice and Gooooooooooooooooooooooooooooood and so excellent tutorial that u explain all in one. i need your some new help. i m beggine rin php, can u please also make php function tutroial. how to insert record using function. Please it a request

  3. wasim says:

    It’s really very nice post for newbies. It’s very clean and understandable for every one.

    Nice One…

  4. zia uddin says:

    please tell me 5 function of php

  5. Koko says:

    $row['0'] is wrong!

  6. Vineet Kumar saini says:

    Ver Great full post, It is very helpful.

  7. venu says:

    It is good

  8. karthik says:

    good.. its the simplest way to understand..

  9. wooxplore says:

    Very Great full post, It is very helpful.
    Thnx

  10. Amrit says:

    Very easily and logically illustrated.

  11. mario says:

    And they’re all outdated.

    Seriously, PDO is significantly simpler. In particular for newbies who are clueless about context and proper SQL escaping. Parameterized queries make your life easier.

  12. Ajay says:

    This is the right way to make one understand the scenario. Great