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
Release: 2024-12-10 20:40:18
Original
799 people have browsed it

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();
}
?>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template