MySQL double while loop in php:
$geta=mysql_query("SELECT id FROM table1 WHERE id<50");
while($row=mysql_fetch_array($geta)){
while($row2=mysql_fetch_array($geta)){
$id1=$row['id'];
$id2=$row2['id'];
echo "$id1 and $id2";
}
}
I’m trying to make every possible combination of id with another id.
Not sure what is wrong with it…output is just a list of id1…
What am I doing wrong here?
Each time you call
mysql_fetch_array(), you fetch the next result from the same result set from the MySQL database. So calling it twice in an outer and inner loop just advances the record pointer.In your case, you should load all results to a single array first, then iterate over that: