Tuesday, 23 April 2013

Different type of Fetching Function in Mysql

All the fetching function is used to fetch data from mysql database. But its data representation and use can be different. No need to confuse while using this function. Programmer can used any of the fetching function below.
For best understanding, refer this table


Different type of fetching function is given below:
1.    MYSQL_FETCH_ROW()
2.    MYSQL_FETCH_ARRAY()
3.    MYSQL_FETCH_ASSOC()
4.    MYSQL_FETCH_OBJECT()


1.    MYSQL_FETCH_ROW():  It is used to fetch data in numeric array. It gives result row as a numeric array.

<?php
$result=mysql_query("select * from user_table");
$result_row=mysql_fetch_row($result);
echo $result_row[0];
echo $result_row[1];
echo $result_row[2];
?>

Result:   1    Discuss   Desk

2.    MYSQL_FETCH_ARRAY(): It is used to fetch data in numeric array as well as associative array . It gives result row as a numeric array as well as associative array.

<?php
$result=mysql_query("select * from user_table");
$result_row=mysql_fetch_row($result);
echo $result_row[0];
echo $result_row[1];
echo $result_row[2];

/* numeric array as well as associative array */

echo $result_row[id];
echo $result_row[first_name];
echo $result_row[last_name];
?>

Both Result:   1    Discuss   Desk

3.    MYSQL_FETCH_ASSOC(): It is used to fetch data in associative array. It gives result row as a associative array.

<?php
$result=mysql_query("select * from user_table");
$result_row=mysql_fetch_row($result);
echo $result_row[id];
echo $result_row[first_name];
echo $result_row[last_name];
?>

Result:   1    Discuss   Desk

4.    MYSQL_FETCH_OBJECT(): It is used to fetch data as object. It gives result row as a object.

<?php
$result=mysql_query("select * from user_table");
$result_row=mysql_fetch_row($result);
echo $result_row->id;
echo $result_row->first_name;
echo $result_row->last_name;
?>

Result:   1    Discuss   Desk


Different type of Fetching Function in Mysql

No comments:

Post a Comment