I have a loop that never ends and its only pulling one record from the database.
I have a class that returns a list then I call that class function on a website where the while loop never stops echoing the data.
class exampleclass
{
public function get_list()
{
$query = $dbh->prepare("SELECT .. blah);
$query->bindParam("blah");
$query->execute();
return $query->fetch(PDO::FetchObj);
}
on the web page i have:
<?php
testobj = new exampleclass();
while ($obj = $testobj->get_list())
{
echo $obj->db_field;
}
?>
It pulls the one record from the database but it doesn't stop looping, it should just loop once.
First of all, quit that idea of creating objects out of DB rows. They look fancy yet totally useless. Then learn how to return whole result and how to properly iterate over it:
public function get_list()
{
$stmt = $dbh->prepare("SELECT .. blah");
$query->execute(array("blah"));
return $query->fetchAll();
}
testobj = new exampleclass();
foreach ($testobj->get_list() as $row)
{
echo $row['db_field'];
}