PHP Record Fetching Functions

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 $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
  1. $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
  2.  
  3. if (!$link)
  4. {
  5. die('Failed to connect to server: ' . mysql_error());
  6. }
  7.  
  8. $db = mysql_select_db(DB_DATABASE);
  9.  
  10. if (!$db)
  11. {
  12. die("Unable to select database");
  13. }
  14.  
  15. $result = mysql_query($query);
  16.  
  17. While($record =mysql_fetch_array($result, MYSQL_ASSOC)){
  18.  
  19. echo $record['user_id'].' , '.$record['user_name'].' , '.$record['email'].' , '.$record['login_name'].' , '.$record['password'];
  20. }
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
  1. $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
  2.  
  3. if (!$link)
  4. {
  5. die('Failed to connect to server: ' . mysql_error());
  6. }
  7.  
  8. $db = mysql_select_db(DB_DATABASE);
  9.  
  10. if (!$db)
  11. {
  12. die("Unable to select database");
  13. }
  14.  
  15. $result = mysql_query($query);
  16.  
  17. While($record =mysql_fetch_array($result, MYSQL_NUM )){
  18.  
  19. echo $record[0].' , '.$record[1].' , '.$record[2].' , '.$record[3].' , '.$record[4];
  20. }
Third parameter is MYSQL_BOTH and this is the default parameter of the function. Records field can be access using above two methods.
  1. $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
  2.  
  3. if (!$link)
  4. {
  5. die('Failed to connect to server: ' . mysql_error());
  6. }
  7.  
  8. $db = mysql_select_db(DB_DATABASE);
  9.  
  10. if (!$db)
  11. {
  12. die("Unable to select database");
  13. }
  14.  
  15. $result = mysql_query($query);
  16.  
  17. While($record =mysql_fetch_array($result, MYSQL_NUM )){
  18.  
  19. echo "This is assoc " .$record['user_id'].' , '.$record['user_name'].' , '.$record['email'].' , '.$record['login_name'].' , '.$record['password'];
  20.  
  21. echo "This is num ".$record[0].' , '.$record[1].' , '.$record[2].' , '.$record[3].' , '.$record[4];
  22. }
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
  1. $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
  2.  
  3. if (!$link)
  4. {
  5. die('Failed to connect to server: ' . mysql_error());
  6. }
  7.  
  8. $db = mysql_select_db(DB_DATABASE);
  9.  
  10. if (!$db)
  11. {
  12. die("Unable to select database");
  13. }
  14.  
  15. $result = mysql_query($query);
  16.  
  17. While($record =mysql_fetch_assoc($result)){
  18.  
  19. echo "This is assoc " .$record['user_id'].' , '.$record['user_name'].' , '.$record['email'].' , '.$record['login_name'].' , '.$record['password'];
  20.  
  21.  
  22. }
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 .
  1. $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
  2.  
  3. if (!$link)
  4. {
  5. die('Failed to connect to server: ' . mysql_error());
  6. }
  7.  
  8. $db = mysql_select_db(DB_DATABASE);
  9.  
  10. if (!$db)
  11. {
  12. die("Unable to select database");
  13. }
  14.  
  15. $result = mysql_query($query);
  16.  
  17. While($record =mysql_fetch_row($result)){
  18.  
  19. echo "This is row ".$record[0].' , '.$record[1].' , '.$record[2].' , '.$record[3].' , '.$record[4];
  20.  
  21. }

Add new comment