给定返回20行的SELECT查询的$结果,完成评论在循环中建议的最简单方法是什么?获取mysql_fetch_assoc循环的数据指针当前位置的最简单方法是什么?
while ($row = mysql_fetch_assoc($result))
{
// echo "success" when i get to the 9th row
}
给定返回20行的SELECT查询的$结果,完成评论在循环中建议的最简单方法是什么?获取mysql_fetch_assoc循环的数据指针当前位置的最简单方法是什么?
while ($row = mysql_fetch_assoc($result))
{
// echo "success" when i get to the 9th row
}
您可以使用mysql_data_seek()
检查第9行存在:
if(mysql_data_seek($result, 8) !== false) {
// success, internal pointer is now at 9
// still need to call mysql_fetch to get the row
}
但你不能在内部指针得到你自己的数。使用for循环保持它有点整洁:
for($i = 0; $row = mysql_fetch_assoc($result); $i++) {
if($i == 8) // success
}
$i = 0; // Or 1 if need be
while ($row = mysql_fetch_assoc($result))
{
// echo "success" when i get to the 9th row
if($i == 9) {
echo "success";
}
$i++;
}
或
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
if($row[8] != '') {
echo "success";
}
}
或
while ($row = mysql_fetch_assoc($result))
{
if($row['column_name_of_the_ninth_field'] != '') {
echo "success";
}
}
在其他的答案已经详细介绍的方法都是有效的方式来做到这一点。
如果你真正希望做的是移动内部指针到第9个结果,而不必经过一个循环,那么这就是你可能会寻找:
if (mysql_data_seek ($result,9)) echo "success";
谢谢。我知道$ i ++是做这件事的一种方式,我只是希望有一种更干净的方式。 – Matthew 2009-10-14 00:03:03