...

View Full Version : mysql_fetch_array bringing up error.



missing-score
04-22-2003, 09:49 PM
Right, this isn't an sql question...

This may be normal, but I just wanted to make sure it is not the way I have scripted my function.




$sql = "SOME QUERY";

$query = mysql_query($sql);

for($i=1; $row = mysql_fetch_array($query); $i++)
{
// Do whatever with the info.
}



In my function, I have it set up with if/else statements to give a report of what happened. It does this fine, until there are no more rows, then it returns an error. Is this becase it is not able to fetch the row, becase it is at the end?

I hope this is clear enough for you.

If you need, I will post the function.

Spookster
04-22-2003, 10:10 PM
You should only ever use a for loop when you know how many times you need to loop. That is why we typically use a while loop to do this:

while ($row = mysql_fetch_array($result)){
//do your stuff
}

You use a while loop as a sentinel controlled loop because you don't know how many times you will need to loop. The sentinel condition being that it stops looping when no more rows are found.

missing-score
04-22-2003, 10:14 PM
Ok, thanks spooks. Ill change it now.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum