Page 1 of 1

setting variables in a foreach loop

Posted: Sun Jun 27, 2004 11:56 am
by snpo123
Hey, is there any special syntax I must use if I am going to define variables in a foreach loop? For example, would something like this work?

Code: Select all

<?php
foreach ($array as $key => $value) {
    $mysql_query = "select $key from users where userid = $value";
    mysql_query ($mysql_query);
}
?>
Any help would be appreciated.
Thanks.

Posted: Sun Jun 27, 2004 12:15 pm
by markl999
would something like this work?
Try it and see? ;)
But yeah, it would work, it wouldn't do much (as you don't do anything with the query result), but it would work.

Posted: Sun Jun 27, 2004 12:16 pm
by snpo123
ok thanks, thats all I needed to know! :)

Posted: Sun Jun 27, 2004 12:16 pm
by kettle_drum
Just use the same syntax as you always would. Theres no point storing the query though as its just an extra command and wastes space - you can just do mysql_query("SELECT.....");