2009-10-13 43 views

回答

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 
} 
+0

谢谢。我知道$ i ++是做这件事的一种方式,我只是希望有一种更干净的方式。 – Matthew 2009-10-14 00:03:03

1
$i=0; 

while ($row = mysql_fetch_assoc($result)) 
{ 
    ++$i; 
    if($i==9) 
     echo "success"; 
} 
+0

反正是有要做到这一点而不涉及$ i?无法访问$ result内的位置? – Matthew 2009-10-13 18:18:03

+0

在我之前缺少$ var以及您在检查之前递增计数器,因此数组索引零将被跳过 – 2009-10-13 18:18:10

+1

我假设他的“第9行”是基于1的,所以我从1开始计数。你对$ tho是正确的,我的PHP是生锈的。 – Blindy 2009-10-13 18:45:14

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"; 
    } 
} 
+1

第二个例子会在$行中引用8,而不是$ result – Matthew 2009-10-13 18:44:26

+0

已修复,应该现在访问第9个数组索引 – 2009-10-13 19:00:01

+0

我想你误解了 – Matthew 2009-10-13 19:11:27

0

在其他的答案已经详细介绍的方法都是有效的方式来做到这一点。

如果你真正希望做的是移动内部指针到第9个结果,而不必经过一个循环,那么这就是你可能会寻找:

if (mysql_data_seek ($result,9)) echo "success"; 
相关问题