SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Jul 2004
    Location
    Brooklyn, NY
    Posts
    316
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to restart mysql_fetch_array while loop

    How do you restart a while mysql_fetch_array loop?

    PHP Code:
    while($row=mysql_fetch_array($result)){
         echo 
    $row['id']."<br>"// will echo all the IDs
    }
    while(
    $row=mysql_fetch_array($result)){ // won't run at all
         
    echo $row['id']."<br>";


  2. #2
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do the query again?

    PHP Code:
    while($row=mysql_fetch_array($result)){
         echo 
    $row['id']."<br>"// will echo all the IDs
    }

    $result mysql_query($query);

    while(
    $row=mysql_fetch_array($result)){ // won't run at all
         
    echo $row['id']."<br>";


  3. #3
    SitePoint Addict
    Join Date
    Jul 2004
    Location
    Brooklyn, NY
    Posts
    316
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah that's the simplest way, but it wouldn't it be more efficient to restart the loop than call the mysql server?

  4. #4
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about this: ? I've never tried it ...

    PHP Code:
     mysql_data_seek($result0); 
    http://ca.php.net/manual/en/function...-data-seek.php

    Red

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,805
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    You could alsways put the results from the query into a non mysql array format

    PHP Code:
    $new_array = array();
    while(
    $row=mysql_fetch_array($result)){
         
    $new_array[] = $row['id'];

    Then you can use that array in any format you like

    PHP Code:
    foreach($new_array as $data) {
    //etc

    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    SitePoint Addict
    Join Date
    Jul 2004
    Location
    Brooklyn, NY
    Posts
    316
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Rederick View Post
    How about this: ? I've never tried it ...

    PHP Code:
     mysql_data_seek($result0); 
    http://ca.php.net/manual/en/function...-data-seek.php

    Red
    Thank you Red, that did the trick!


Bookmarks

Posting Permissions

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