PHP Record Fetching Functions
Submitted by thusitcp on Monday, October 6, 2014 - 10:49.
PHP Record Fetching Functions
Today world wide web is dominated by php applications. Over 250M sites use php on the web and php 5 used 98% sites who use php. Most of php based application use mysql database server to store application related data. Nowadays software application contained millions of records because of that data retrieval is getting complex.
In this tutorial I am going to explain mysql result manipulation functions and their usage. Everyone wants their application run faster since selection of correct database query and handling result going to be play vital role. In this tutorial I am not going to explain about how to select optimal database query. my main objective is to explain result fetching php functions .
There are three major php record fetching function
1. mysql_fetch_array
2. mysql_fetch_assoc
3. mysql_fetch_row
I am going to explain above functions using user table and it has following structure
User(user_id,user_name,email,login_name,password)
Following query will be used to selecting record from user table
This will print each record of the result . Now you can notice when we use mysql_assoc parameter we are access return array using key value pair
Next we going to use MYSQL_NUM which producing numerical array following code line showing usage of function with MYSQL_NUM parameter
Third parameter is MYSQL_BOTH and this is the default parameter of the function. Records field can be access using above two methods.
Major drawback of this function is relatively slower than other functions. mysql_fetch_array consume greater execution time. When we have larger record set this function is performing poorly relatively other functions.
2. mysql_fetch_assoc
mysql_fetch_assoc function fetch the result row as associative array so The function is returning value as key value pair. This function is also slower than mysql_fetch_row function
usage of function as follows
3. mysql_fetch_row
This function is highly optimize function. Returning result as a numerical array and faster than above mention two function. This function return next row as null if there is no more rows to retrieve as a result of an error. Usage of function as follows .
$query = “select user_id, user_name,email,login_name,password from user”;
1.mysql_fetch_array
function has following syntax
mysql_fetch_array( $array, $array_type);
its parameter description as follows
$array = result set return by the mysql_query function
$array_type = type of output array from the function which has following value
MYSQL_ASSOC = result return as assosiative array
MYSQL_NUM = array return as numeric array
MYSQL_BOTH = return both of above and this is the default setting
Usage
- if (!$link)
- {
- }
- if (!$db)
- {
- }
- echo $record['user_id'].' , '.$record['user_name'].' , '.$record['email'].' , '.$record['login_name'].' , '.$record['password'];
- }
- if (!$link)
- {
- }
- if (!$db)
- {
- }
- echo $record[0].' , '.$record[1].' , '.$record[2].' , '.$record[3].' , '.$record[4];
- }
- if (!$link)
- {
- }
- if (!$db)
- {
- }
- echo "This is assoc " .$record['user_id'].' , '.$record['user_name'].' , '.$record['email'].' , '.$record['login_name'].' , '.$record['password'];
- echo "This is num ".$record[0].' , '.$record[1].' , '.$record[2].' , '.$record[3].' , '.$record[4];
- }
- if (!$link)
- {
- }
- if (!$db)
- {
- }
- echo "This is assoc " .$record['user_id'].' , '.$record['user_name'].' , '.$record['email'].' , '.$record['login_name'].' , '.$record['password'];
- }
- if (!$link)
- {
- }
- if (!$db)
- {
- }
- echo "This is row ".$record[0].' , '.$record[1].' , '.$record[2].' , '.$record[3].' , '.$record[4];
- }
Add new comment
- 31 views