Multi-Row Retrieval and Iteration in MySQL and PHP
In the context of accessing multiple rows from a MySQL database, the question arises on how to retrieve and use them effectively in PHP. Consider a database table with several columns, including "number1" and "number2." To query this table and select rows where "number1" equals 1, one might utilize the mysql_fetch_assoc() function. However, this function only returns the first matching row.
To overcome this limitation and retrieve multiple rows, a simple solution is to invoke mysql_fetch_assoc() repeatedly for each desired row. The PHP documentation offers a clear example of this approach:
<code class="php">while ($row = mysql_fetch_assoc($result)) { echo $row["userid"]; echo $row["fullname"]; echo $row["userstatus"]; }</code>
This code snippet iterates through all rows in the $result variable, extracting each row as an associative array. The values stored within the array can then be accessed using their respective column names.
By employing this method, it is possible to retrieve and iterate over multiple rows efficiently, allowing for further processing or data manipulation in PHP scripts.
The above is the detailed content of How to Retrieve and Iterate Over Multiple Rows in MySQL and PHP?. For more information, please follow other related articles on the PHP Chinese website!