Multiple PHP WHILE loops using the same query

$query1 = "SELECT * FROM idevaff_affiliates";
$affiliateID = mysql_query($query1) or die(mysql_error());

This is my request above - I would like to use it for two WHILE loops

The first in the header section is jquery setup

while($row = mysql_fetch_assoc($affiliateID)){
}

The second is used in a while loop in the body

while($row = mysql_fetch_assoc($affiliateID)){
}

Why can't I make it work? โ€œI got this, but I had to make two queries using the same SELECT information using two different variables.โ€

+5
source share
3 answers

The call mysql_fetch_assoc()returns the next line (i.e. the next that you have not received yet). Having received all the rows, it returns false. So, as soon as you went through this first cycle, you got all the lines, and everything that you return will be falseevery time!

, , ?

$rows = array();
while($row = mysql_fetch_assoc($affiliateID)){ 
    $rows[] = $row;
}

$rows , :

foreach($rows as $row) { ... }
+11

, , . , mysql_data_seek reset .

+6

You can search for the first row ... using mysql_data_seek ($ affiliateID, 0), now you can run mysql_fetch_assoc () again ... anyway, I prefer to load the result into an array.

+1
source

All Articles