Results 1 to 5 of 5

Thread: Get rid in while (fetch_array)

  1. #1

    Thread Starter
    Fanatic Member aNubies's Avatar
    Join Date
    Aug 2008
    Posts
    558

    Get rid in while (fetch_array)

    How to get rid in the while (mysql_fetch_array) i use the exit but did not work.

    In this code "exit" works.
    Code:
        while ($counter < 10)
         {
            if ($counter == 5)
            {
                 exit;
            }else
            {
                 echo $counter
            }
         }
    While in this code "exit" doesn't work. Cause when the codition meets it supposed to stop the while statement, but it wont stop it will continue the looping.
    Code:
           while ($fields = mysql_fetch_array($sql))
           {
                if ($fname == $fields['fname'] && $lname == $fields['lname'])
                {
                     echo "welcome"
                     exit;
                }
           }

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Get rid in while (fetch_array)

    "exit" will kill the script. the command you want to look at is "break." the other similar command is "continue." break will break out of your loop, and continue will continue on in your loop.

    PHP Code:
    <?php
      $i 
    0;
      while(
    $i 200){
        
    $i++;
        if(
    $i == 5)
          break;

        echo 
    $i;
      }
    ?>
    that code should produce the following: 1234
    Last edited by kows; May 20th, 2009 at 02:01 AM.

  3. #3

    Thread Starter
    Fanatic Member aNubies's Avatar
    Join Date
    Aug 2008
    Posts
    558

    Re: Get rid in while (fetch_array)

    No i know that i referring in how to get rid in this code

    Code:
           while ($fields = mysql_fetch_array($sql))
           {
                if ($fname == $fields['fname'] && $lname == $fields['lname'])
                {
                     echo "welcome"
                     exit;
                }
           }
    Cause in their even though its already true in the condition, the loop will continue so how to stop that

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Get rid in while (fetch_array)

    the loop will not continue if the condition is true in your IF statement because "exit" tells PHP to stop processing the script. I'm not sure what you're talking about.

  5. #5
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Get rid in while (fetch_array)

    Do what kows said: put break instead of exit.


    Has someone helped you? Then you can Rate their helpful post.

Posting Permissions

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



Click Here to Expand Forum to Full Width