The PDO Class Queries and Processing of Results
Processing Result Sets
•
We can use a while-loop together with the fetch() method to iterate
over all rows in a result set
while ( $row = $result - > fetch ()) {
echo " Slot : " , $row [ " slot " ], " <br >\ n ";
echo " Name : " , $row [ " name " ], " <br >\ n ";
echo " Email : " , $row [ " email "]," <br >< br >\ n" ;
}
•
Alternatively, we can use a foreach-loop
foreac h ( $result as $ row ) {
echo " Slot : " , $row [ " slot " ], " <br >\ n ";
echo " Name : " , $row [ " name " ], " <br >\ n ";
echo " Email : " , $row [ " email "]," <br >< br >\ n" ;
}
•
fetch() uses a cursor that moves through the rows in a result set and
does not reset at the end
; store the result set in an array first (e.g., using fetchAll() ),
then iterate over the array as often as you want
COMP519 Web Programming Lecture 25 Slide L25 – 19