[thelist] PHP & MySQL, query in a loop

Kasimir K evolt at kasimir-k.fi
Tue Apr 6 07:20:30 CDT 2004


Hola Amigos,

I've got a loop:
for ($i = 1; $i < count($groups); $i++) {
    $groups_idkey[$groups[$i]['id']] = $groups[$i]['name'];
    $groups[$i]['text_ids'] = get_1d_array_from_db_query("
       SELECT id
       FROM texts
       JOIN text_x_group ON texts.id = text_x_group.text_id
       WHERE group_id = '".$groups[$i]['id']."'
       ORDER BY order_number ASC
    ");
    array_unshift($groups[$i]['text_ids'], '0');
}

and the function get_1d_array_from_db_query() looks like this:
function get_1d_array_from_db_query($mysql_query) {
    $rs = mysql_query($mysql_query)
       or die(mysql_error()."<br><br><pre>".$mysql_query."</pre>");
    $array = array();
    while ($row = mysql_fetch_array($rs)) array_push($array, $row[0]);
    return $array;
}

When I operate on a db-server which is on the same computer as apache, 
there's no problems, but if I operate on my local apache, and remote 
mysql, the loop never reaches the end... but that's not all... to debug 
this, I put after the array_unshift line the following:
echo $i;
print_r($groups[$i]);
the loop dies on the 22nd round (of 34), but if I debug with this:
echo $i.str_repeat('=',100);
print_r($groups[$i]);
the loop dies on the last round (35)! This made me think, that the 
remote mysql server wants some time in between queries, and tried this 
for debugin:
sleep(1);
echo $i;
print_r($groups[$i]);
which resulted the script being slower, and dying after 22 rounds...

Please, tell me what's going on here!

.k


More information about the thelist mailing list