当前位置:Gxlcms > PHP教程 > 为何mysql_fetch_array/assoc+foreach只能遍历一条记录

为何mysql_fetch_array/assoc+foreach只能遍历一条记录

时间:2021-07-01 10:21:17 帮助过:6人阅读

为什么mysql_fetch_array/assoc + foreach只能遍历一条记录
foreach(mysql_fetch_array($result) as $key=>$row){

print_r($row);

}


这样只能遍历输出最后一条记录

下面这样就能全部遍历,是不是说明数据库返回的数组与平常的不同?

while($row=mysql_fetch_array($result)) 
{
$rows[] = $row;
}

foreach($rows as $k => $v)
{
foreach($v as $vv){
echo $vv;
}
mysql_fetch_array ?foreach

分享到:


------解决方案--------------------
while 可以循环读取数据库中的记录,而foreach 只能遍历已存在的数组或者对象。

人气教程排行