Home > Database > Mysql Tutorial > How to Retrieve and Access Multiple MySQL Rows in PHP Using Nested Arrays?

How to Retrieve and Access Multiple MySQL Rows in PHP Using Nested Arrays?

Susan Sarandon
Release: 2024-10-31 21:11:29
Original
412 people have browsed it

How to Retrieve and Access Multiple MySQL Rows in PHP Using Nested Arrays?

Retrieving Multiple MySQL Rows in PHP Using Nested Arrays

To retrieve multiple MySQL rows and access them in PHP, you can utilize the repeated calls to the mysql_fetch_assoc() function. As specified in the PHP documentation:

mysql_fetch_assoc() retrieves the next row from the result set as an associative array. The array keys are the field names, and the array values are the corresponding field values.

To access the data from multiple rows, you can use a nested array. Here's an example:

<code class="php"><?php
// Establish database connection and execute query to select rows with number1 = 1

// Repeatedly call mysql_fetch_assoc() to retrieve each row as an associative array
while ($row = mysql_fetch_assoc($result)) {
    // Store each row in a nested array
    $multidimensionalArray[] = $row;
}

// Access data from the nested array using the following syntax:
$firstRowData = $multidimensionalArray[0];
$secondRowData = $multidimensionalArray[1];
echo $firstRowData['number2']; // Prints the number2 value for the first row
echo $secondRowData['number2']; // Prints the number2 value for the second row
?></code>
Copy after login

This approach allows you to iterate through the rows and access the specific data you need, even from multiple rows, using the nested array structure.

The above is the detailed content of How to Retrieve and Access Multiple MySQL Rows in PHP Using Nested Arrays?. 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