I'm confused about how these snippets of code differ in displaying results in a while loop. The first snippet of code works perfectly but the second displays the results but does so in an infinite loop and just repeats the results. Can someone explain to me why?
$stmt = $db_conn->prepare($sql);
$stmt->execute(array("Lorna", 3));
while($result = $stmt->fetch()){
echo $result["name"] . "<br />" . $result["description"] . "<br />";
}
If I put $stmt->fetch() into a variable called $data and try to pass this instead of just putting $stmt->fetch() in the while loop, I get an infinite loop.
$stmt = $db_conn->prepare($sql);
$stmt->execute(array("Lorna", 3));
$data = $stmt->fetch();
while($result = $data){
echo $result["name"] . "<br />" . $result["description"] . "<br />";
}
Thanks in advance!