Home > Database > Mysql Tutorial > body text

How to Retrieve and Access Multiple MySQL Rows in PHP?

Susan Sarandon
Release: 2024-11-01 04:28:27
Original
588 people have browsed it

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;
Copy after login

}

// 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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!