fetchAll() // Returns an array for all of the rows in the result set.
$query = "SELECT productCode, productName, listPrice FROM products WHERE categoryID = :category_id;" $statement = $db->prepare($query); $statement->bindValue(":category_id", $category_id); $statement->execute(); $products = $statement->fetchAll(); $statement->closeCursor();
<!-- HTML code to begin a table goes here --> <?php foreach ($products as $product) { ?> <td><?php echo $product['productCode']; ?></td> <td><?php echo $product['productName']; ?></td> <td><?php echo $product['listPrice']; ?></td> </tr> <?php } ?> <!-- HTML code to end table goes here -->
fetchAll()
method to return an array that contains that result set. Then, you can use the closeCursor()
method to close the connection to the database.foreach
statement to define a foreach
loop that processes one element of the array each time through the loop.foreach
statement makes it easier to use the foreach
statement with other control statements because it doesn’t require the use of braces.if
statement with an endif
statement.