I have a MySql class and I fetch the Mysql rows by returning them as a method:
public function fetch_assoc($result_set){
return mysql_fetch_assoc($result_set);
}
For some reason it only return one result when I try to iterate through an method return through the object I have instantiated.
while ($row = $a->fetch_assoc($result_set){
...
}
While doing the old fashion ways works, and gives me all the rows
while($row = mysql_fetch_array($result_set)){
$row['0'];
}
Any ideas?
That is because mysql_fetch_assoc return only one result and moves the pointer to the next. So at each iteration it returns moves the pointer and returns false when it reaches the end of the results.
You can modify your method this way to make it work:
And you can use the data as so:
Note that in this case
$obj->fetch_assoc($result_set)returns all the results, and i am just looping through it using a foreach loop;