How to access mysql result set data with a foreach loop
To access MySQL result set data with a foreach loop, you can use a loop like the following:
<?php
$result = mysqli_query($conn, $query);
foreach($result as $row) {
// process each row
}
In this example, $conn
is a connection to the MySQL database, and $query
is the query that you want to execute. The result set is stored in the $result
variable.
Inside the loop, $row
will be an associative array that represents a single row in the result set. You can access the individual columns of the row by using their keys. For example:
<?php
foreach($result as $row) {
echo "ID: " . $row['id'];
echo "Name: " . $row['name'];
}
This will loop through all the rows in the result set, and print out the id
and name
column values for each row.
Note that this code assumes that you are using the MySQLi extension for PHP. If you are using a different MySQL library, the syntax may be slightly different.