MySQL Fetch Array in PHP

The MySQL fetch array function is used to process the result set we get after running a query on the database. The data that comes from MySQL cannot be directly manipulated which and this is where the fetch array function comes in.

The mysql_fetch_array function basically extracts data row by row from the result set returned by MySQL.

The MySQL tutorial below will help get you up to speed on this useful function.

MySQL Fetch Array Tutorial

Below is the syntax for the MySQL Fetch Array function:

$row = mysql_fetch_array($result);

After we have run a query on the database that returns some rows, we pass the data to the function and it will extract the data row by row as necessary. The function returns the rows (of the resource that is passed to it) in the form of an associative array. The values in this associative array can be extracted by using the column names as they were in the database.

For the following examples we will assume that we have a database called "employees" and it contains an "employees" table as follows:

first_name last_name
Paul Pitterson
Francine Beecham
Raul DiNozzo

MySQL Fetch Array Example Without Loops

Take a look at this simple example:

<?php
 
// make a connection
$connection = mysql_connect("localhost", "root", "a");
 
// select the database that we will be using
mysql_select_db("employees");
 
// build and execute the query
$sql = "SELECT * FROM employees";
$result = mysql_query($sql);
 
// process each request
$row = mysql_fetch_array($result);
echo $row['first_name']. " - ". $row['last_name'];
echo "<br />";
 
// process each request
$row = mysql_fetch_array($result);
echo $row['first_name']. " - ". $row['last_name'];
echo "<br />";
 
// process each request
$row = mysql_fetch_array($result);
echo $row['first_name']. " - ". $row['last_name'];
echo "<br />";
 
// close the connection
mysql_close($connection);
 
?>

After a long and tedious (can you imagine if you had 100 rows?) process the script would return:

Paul - Pitterson
Francine - Beecham
Raul - DiNozzo

MySQL Fetch Array Example Using Loops

There is an easier way. A useful feature of the mysql_fetch_array function is the fact that each time it is called it returns the next row of the result. The good thing is that when there are no more rows to return, the function will return false. How is that good? It is good because we can use the function as the condition for a while loop, thus causing the loop to iterate through all rows and then automatically terminate when finished.

Here is a better example of using the MySQL fetch array command:

<?php
 
// make a connection
$connection = mysql_connect("localhost", "apache", "foobar");
 
// select the database that we will be using
mysql_select_db("employees");
 
// build and execute the query
$sql = "SELECT * FROM employees";
$result = mysql_query($sql);
 
// iterate through the results
while ($row = mysql_fetch_array($result))
{
  echo $row['first_name']. " - ". $row['last_name'];
  echo "<br />";
}
 
// close the connection
mysql_close($connection);
 
?>

The result would be the same as above but now our code is more elegant and more powerful:

Paul - Pitterson
Francine - Beecham
Raul - DiNozzo

Using a while loop as we did above to process the result of the mysql_fetch_array command is the quickest and preferred way of processing data from a query. It allows us to get the job done without needing to know or care how many results were returned from the query.

We hope this PHP tutorial was useful to you.

Link to this Page

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...