Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, thanks spooks. Ill change it now.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •