I want to loop multiple times through my query results, and I have 2 ways in mind:
1-
while($data = mysql_fetch_array($res , MYSQL_ASSOC)
{
array_push($new_arr , $data);
}
foreach($sites as $s){
foreach($new_arr as $d)
{
//some code
}
}
2-
foreach($sites as $s){
while($data = mysql_fetch_array($res , MYSQL_ASSOC)
{
//some code
}
mysql_data_seek($res,0);//set the pointer
}
My question: I think option 2 is more efficient but what if I lose connection to db during the mysql_fetch_array(option 2)? I get allot of results; this will take around 10-20 minutes. Will that affect the query results($res) or are they safe?
The data is mapped into a buffer assigned by the mysql client when you execute mysql_query() therefore losing the connection is irrelevant. The first method you cite will create a further copy of the data as a PHP array – which is not very efficient.
Also, WTF is the nested loop ($sites) doing? It looks like either your data is not mormalized or you’ve got a very bad algorithm.