Home >Database >Mysql Tutorial >How Do I Iterate Through a MySQL Result Set in PHP?

How Do I Iterate Through a MySQL Result Set in PHP?

Linda Hamilton
Linda HamiltonOriginal
2024-12-10 20:40:18762browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn