May 25, 2014

Ketan Patel

difference between mysql_fetch_array and mysql_fetch_assoc and mysql_fetch_row

Difference between mysql_fetch_array and mysql_fetch_row



Many of the PHP newbies get confused with mysql_fetch_row(), mysql_fetch_object(), mysql_fetch_assoc(), mysql_fetch_array() functions.Basically all of these functions performs with a similar process.




Let's create a table called "users" with three fields 'id','username','email' having values like  '1','alex','test@test.com'.We have simple index.php file in which we're going to use these different functions.


mysql_fetch_row()


Fetch result row as an numeric way.This function will return a row where the values will come in the order as they are defined in the SQL query, and the keys will span from 0 to one less than the number of columns selected.

<?php
mysql_connect("localhost", "root", "password");
mysql_select_db("test");
$result = mysql_query("select * from users");
while ($rows = mysql_fetch_row($result))
{
    echo $userid=$rows[0];
    echo "<br>";
    echo $username=$rows[1];
    echo "<br>";
    echo $useremail=$rows[2];
}
?>

Output:

1
alex
test@test.com


Here with this  mysql_fetch_row() function , if you are fetching the selected records then their order matters while getting the result.

For example if you have written, "select id, username,email from users" 
then,

$rows[0]; will have the value of id
$rows[1]; will have the value of username
$rows[2]; will have the value of email

but if you write ,"select id, email,username from users" 
then,

$rows[0]; will have the value of id
$rows[1]; will have the value of email
$rows[2]; will have the value of username


mysql_fetch_assoc()


Fetch a result row as an associative array.This function will return a row as an associative array where the column names will be the keys storing corresponding value.

<?php
mysql_connect("localhost", "root", "password");
mysql_select_db("test");
$result = mysql_query("select  * from users");
while ($rows = mysql_fetch_assoc($result))
{
    echo $userid=$rows['id'];
    echo "<br>";
    echo $username=$rows['username'];
    echo "<br>";
    echo $useremail=$rows['email'];
}
?>


Output:

1
alex
test@test.com



mysql_fetch_array()


Fetch a result row as an associative array, a numeric array and also it fetches by both associative & numeric array.This function will actually return an array with both the contents of mysql_fetch_row and mysql_fetch_assoc merged into one. It will both have numeric and string keys.

<?php
mysql_connect("localhost", "root", "password");
mysql_select_db("test");
$result = mysql_query("select  * from users");
while ($rows = mysql_fetch_array($result))
{
    echo $userid=$rows['id'];
    echo "<br>";
    echo $username=$rows['username'];
    echo "<br>";
    echo $useremail=$rows['email'];

/* Now here both associative array and numeric array will work. */

    echo $userid=$rows[0];
    echo "<br>";
    echo $username=$rows[1];
    echo "<br>";
    echo $useremail=$rows[2];

}
?>

Output:

1
alex
test@test.com



mysql_fetch_object()


Fetch a result row as an object

<?php
mysql_connect("localhost", "root", "password");
mysql_select_db("test");
$result = mysql_query("select  * from users");
while ($rows = mysql_fetch_object($result))
{
    echo $userid=$rows->id;
    echo "<br>";
    echo $username=$rows->username;
    echo "<br>";
    echo $useremail=$rows->email;
?>

Output:

1
alex
test@test.com



Thats it..!!.
I hope now you all are very clear with all these functions.

Ketan Patel

About Ketan Patel -

I have developed a wide range of websites using CorePHP, Opencart, CakePHP and CodeIgniter including sites for startup companies and small businesses. Apart from my blogging life, I like to read Novels, Listening music and Net surfing.

Subscribe to this Blog via Email :