setting variables in a foreach loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
snpo123
Forum Commoner
Posts: 77
Joined: Sat Apr 17, 2004 6:31 pm

setting variables in a foreach loop

Post 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.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
User avatar
snpo123
Forum Commoner
Posts: 77
Joined: Sat Apr 17, 2004 6:31 pm

Post by snpo123 »

ok thanks, thats all I needed to know! :)
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.....");
Post Reply