Home  >  Article  >  Database  >  How to Retrieve and Access Multiple MySQL Rows in PHP?

How to Retrieve and Access Multiple MySQL Rows in PHP?

Susan Sarandon
Susan SarandonOriginal
2024-11-01 04:28:27593browse

How to Retrieve and Access Multiple MySQL Rows in PHP?

Retrieving Multiple MySQL Rows and Accessing Them in PHP

To address the issue of retrieving multiple rows from a MySQL database and utilizing them in PHP, you can leverage the mysql_fetch_assoc() function. As the documentation suggests, you can iteratively call this function to access each row as an associative array.

Here's an example:

$query = "SELECT * FROM records WHERE number1 = 1";
$result = mysql_query($query);

$rows = array();

// Iterate through the results and store each row in the $rows array
while ($row = mysql_fetch_assoc($result)) {

$rows[] = $row;

}

// Access the second row using the array index
$secondRow = $rows[1];

// Print the value of number2 for the second row
echo $secondRow['number2'];
?>

Using this approach, you can store all the rows returned by the query in an array and then access them individually based on their position in the array.

The above is the detailed content of How to Retrieve and Access Multiple MySQL Rows 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