给定一个返回20行的SELECT查询的$ result,实现注释在循环中提出的内容的最简单方法是什么?
while ($row = mysql_fetch_assoc($result))
{
// echo "success" when i get to the 9th row
}
答案 0 :(得分:1)
$i=0;
while ($row = mysql_fetch_assoc($result))
{
++$i;
if($i==9)
echo "success";
}
答案 1 :(得分:1)
您可以使用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
}
答案 2 :(得分:0)
$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";
}
}
答案 3 :(得分:0)
其他答案中已经详述的方法都是有效的方法。
如果您真正想做的是将内部指针移动到第9个结果而不必经过循环,那么这就是您可能正在寻找的内容:
if ( mysql_data_seek ($result,9) ) echo "success";