Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-22-2003, 08:49 PM   PM User | #1
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
mysql_fetch_array bringing up error.

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.

PHP Code:

$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.
missing-score is offline   Reply With Quote
Old 04-22-2003, 09:10 PM   PM User | #2
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,225
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
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.
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 04-22-2003, 09:14 PM   PM User | #3
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
Ok, thanks spooks. Ill change it now.
missing-score is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:12 AM.


Advertisement
Log in to turn off these ads.