Home » , » variation between mysql_fetch_array & mysql_fetch_assoc & mysql_fetch_row

variation between mysql_fetch_array & mysql_fetch_assoc & mysql_fetch_row

Written By Unknown on June 23, 2014 | Monday, June 23, 2014

Many of the PHP developer 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 its called "user" with three fields 'id','username','email' having values like '1','andrew','andrew@gmail.com'.We have simple index.php file in which we're going to use these differentiate all  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 MY-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 "";
    echo $username=$rows[1];
    echo "";
    echo $useremail=$rows[2];
  }
?>

Output

1
andrew
andrew@gmail.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 "";
    echo $username=$rows['username'];
    echo "";
    echo $useremail=$rows['email'];
}
?>
Output:

Output

1
andrew
andrew@gmail.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 "";
    echo $username=$rows['username'];
    echo "";
    echo $useremail=$rows['email'];

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

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

}
?>

Output

1
andrew
andrew@gmail.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 "";
    echo $username=$rows->username;
    echo "";
    echo $useremail=$rows->email;
?>

Output

1
andrew
andrew@gmail.com

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

0 comments:

Post a Comment