MySQL Fetch Array Function in PHP

Introduction
 
In this article I explain the "MySQL_Fetch_Array()" function in PHP. This function fetches the data from your table. The MySQL_fetch_array() function fetches the result row as an associative array, a numeric array, or both and gets a row from the MySQL_query() function and returns an array on success.
 
Syntax
 
MySQL_fetch_array(data,Array_type);

Parameter
dataRequired. Specifies which data pointer to use. The data pointer is the result from a MySQL_Query() function.
Array_typeThe type of the array to be fetched. It is a constant and can use the following values MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH.
 
Returns an array corresponding to the fetched row and this function returns field names that are case-sensitive.
 
MySQL_Fetch_Array with MySQL_NUM
  1. <?php  
  2. MySQL_connect("localhost""root"""or  
  3. die("Could not connect: " . MySQL_error());  
  4. MySQL_select_db("smart");  
  5. $rs = MySQL_query("SELECT c_id, c_name FROM courses");  
  6. while ($row = MySQL_fetch_array($rs, MYSQL_NUM))  
  7. {  
  8. echo "<pre>";  
  9. printf("ID: %s Name: %s"$row[0], $row[1]);  
  10. }  
  11. MySQL_free_result($rs);  
  12. ?>  
Output
 
 
 
MySQL_Fetch_Array with MySQL_ASSOC
  1. <?php  
  2. MySQL_connect("localhost""root"""or  
  3. die("Could not connect: " . MySQL_error());  
  4. MySQL_select_db("smart");  
  5. $rs = MySQL_query("SELECT c_id, c_name FROM courses");  
  6. while ($row = MySQL_fetch_array($rs, MYSQL_ASSOC))  
  7. {  
  8. echo "<pre>";  
  9. printf("ID: %s Name: %s"$row['c_id'], $row['c_name']);  
  10. }  
  11. MySQL_free_result($rs);  
  12. ?>  
Output
 
 
MySQL_Fetch_Array with MySQL_BOTH
  1. <?php  
  2. MySQL_connect("localhost""root"""or  
  3. die("Could not connect: " . MySQL_error());  
  4. MySQL_select_db("smart");  
  5. $rs = MySQL_query("SELECT c_id, c_name FROM courses");  
  6. while ($row = MySQL_fetch_array($rs, MYSQL_BOTH))  
  7. {  
  8. echo "<pre>";  
  9. printf("ID: %s Name: %s"$row[0], $row['c_name']);  
  10. }  
  11. MySQL_free_result($rs);  
  12. ?>  
Output
 


Similar Articles