[英]mysql fetch return only return most recent row in class
I have a MySql class and I fetch the Mysql rows by returning them as a method: 我有一个MySql类,并通过将它们作为方法返回来获取Mysql行:
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. 那是因为mysql_fetch_assoc只返回一个结果并将指针移到下一个结果。 So at each iteration it returns moves the pointer and returns false when it reaches the end of the results. 因此,在每次迭代中,它返回都会移动指针,并在到达结果结尾时返回false。
You can modify your method this way to make it work: 您可以通过以下方式修改您的方法以使其起作用:
public function fetch_assoc($result_set){
$resultArray = array();
while ($row = mysql_fetch_assoc($result_set){
$resultArray[] = $row;
}
return $resultArray;
}
And you can use the data as so: 您可以这样使用数据:
foreach ($obj->fetch_assoc($result_set) as $row){
echo $row['stuff'];
}
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; 注意,在这种情况下, $obj->fetch_assoc($result_set)
返回所有结果,而我只是使用foreach循环遍历它;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.