Given a $result from a SELECT query that returned 20 rows, what is the simplest way to accomplish what the comment sugggests in the loop?
while ($row = mysql_fetch_assoc($result))
{
// echo "success" when i get to the 9th row
}
Given a $result from a SELECT query that returned 20 rows, what is the simplest way to accomplish what the comment sugggests in the loop?
while ($row = mysql_fetch_assoc($result))
{
// echo "success" when i get to the 9th row
}
$i=0;
while ($row = mysql_fetch_assoc($result))
{
++$i;
if($i==9)
echo "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++;
}
or
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
if($row[8] != '') {
echo "success";
}
}
or
while ($row = mysql_fetch_assoc($result))
{
if($row['column_name_of_the_ninth_field'] != '') {
echo "success";
}
}
You can use mysql_data_seek() to check if the 9th row exists:
if(mysql_data_seek($result, 8) !== false) {
// success, internal pointer is now at 9
// still need to call mysql_fetch to get the row
}
but you can't get at the internal pointer short of counting yourself. Using a for loop keeps it a bit tidier:
for($i = 0; $row = mysql_fetch_assoc($result); $i++) {
if($i == 8) // success
}
the methods detailed already in the other answers are all valid ways to do it.
if what you're really looking to do is to move the internal pointer to the 9th result without having to go through a loop, then this is what you may be looking for:
if ( mysql_data_seek ($result,9) ) echo "success";