Home >Database >Mysql Tutorial >How Do I Iterate Through a MySQL Result Set in PHP?
Looping Through a MySQL Result Set
Looping through a MySQL result set is a common task when working with databases in PHP. There are several different ways to achieve this.
mysql_fetch_array() Method
One method is to use the mysql_fetch_array() function. This function fetches the next row from the result set and returns an associative array containing the column names as keys and the column values as values. The following code shows an example of how to use this method:
<?php $link = mysql_connect(/*arguments here*/); $query = sprintf("select * from table"); $result = mysql_query($query, $link); if ($result) { while($row = mysql_fetch_array($result)) { // do something with the $row } } else { echo mysql_error(); } ?>
The above is the detailed content of How Do I Iterate Through a MySQL Result Set in PHP?. For more information, please follow other related articles on the PHP Chinese website!