Why does this function return an infinite loop? I am trying to create a function where you just have to give it the query and the while loop takes care of the rest. but for some reason ONLY when i have the function return $q->fetch_assoc() it goes into an infinite loop. if I return just $q, then call $q->fetch_assoc() in the while loop then it doesn't cause the infinite loop.
public function fetch($query){
$con = $this->con;
$q = $con->query($query);
return $q->fetch_assoc();
}
while($r = $sqli->fetch("SELECT id FROM users LIMIT 5")){
echo $r['id']."<br />";
}
also tried
public function fetch($query){
$con = $this->con;
$q = $con->query($query);
return $q->fetch_assoc();
}
$x = $sqli->fetch("SELECT id FROM users LIMIT 5");
while($r = $x){
echo $r['id']."<br />";
}
I thought maybe it was recreating a new fetch function every time. This too did not work and caused the infinite loop.