Use query twice mysql_fetch_array

后端 未结 5 1787
忘了有多久
忘了有多久 2020-12-29 06:06

a simple

$stuff = mysql_query(\"SELECT * FROM users\");

while($s = mysql_fetch_array($stuff)){
# ....
}

while($r = mysql_fetch_array($stuff)){
# ...
}


        
相关标签:
5条回答
  • 2020-12-29 06:26

    If you user "foreach" instead of "while" you'll not have any problem and no need to mysqli_seek_data() :

    //1st loop :
    foreach($stuff as $row)
    {
       echo $row['...'];
    }
    ...
    //2nd loop :
    foreach($stuff as $row)
    {
       echo $row['...'];
    }
    
    0 讨论(0)
  • 2020-12-29 06:31

    Here is another simpler solution:

    $slq = "SELECT * FROM users";
    
    $stuff1 = mysql_query($slq);
    
    while($s = mysql_fetch_array($stuff1)){
    # ....
    }
    
    $stuff2 = mysql_query($slq);
    
    while($r = mysql_fetch_array($stuff2)){
    # ...
    }
    

    Assign the sql to a variable, and call mysql multiple times.

    no more mysql. use mysqli or pdo

    0 讨论(0)
  • 2020-12-29 06:39
    $stuff = mysql_query("SELECT * FROM users");
    
    while($s = mysql_fetch_array($stuff)){
    # ....
    }
    // add this line
    mysql_data_seek( $stuff, 0 );
    
    while($r = mysql_fetch_array($stuff)){
    # ...
    }
    

    Should do the trick

    Another way is of course to store your result in an array and reuse that

    0 讨论(0)
  • 2020-12-29 06:39

    see the docs

    I guess it's because

    Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

    I think moving data pointer with mysql_data_seek() would do the job But i think this is not a good way, you should usually fetch data from database once and store them

    0 讨论(0)
  • 2020-12-29 06:45
    $stuff = mysql_query("SELECT * FROM users");
    
    while($s = mysql_fetch_array($stuff)){
    # ....
    }
    mysql_data_seek($stuff, 0);
    while($r = mysql_fetch_array($stuff)){
    # ...
    }
    //ref: http://www.php.net/manual/en/function.mysql-data-seek.php
    
    0 讨论(0)
提交回复
热议问题