Page 1 of 1

Trouble embedding PHP variables in a MySQL query...

Posted: Tue Jul 12, 2005 7:21 pm
by bwv2
I know it's simple, but I can't seem to figure this out. I am looping through a few lines of code in order to pull certain data from a mysql table. Each column of the table has a similar name, only differing by a character. I want to have that character change each loop, like the following:

Code: Select all

for($aaa=0; $aaa<=$max; $aaa++){
$sql = "SELECT first{$aaa}last FROM mytable WHERE id=$aaa";
$result = mysql_query($sql);
$myvalue = mysql_result($result,0,0);
}
I know I'm not using the correct syntax to get the $aaa variable embedded into the word "first***last". Ideally, the output of this loop would be, for $max=4:
  • first0last
    first1last
    first2last
    first3last
    first4last
What do I need to do to get this syntax right? Thanks.

Posted: Tue Jul 12, 2005 7:37 pm
by Burrito
you do know you're creating 5 different queries right?

you're also resetting the $sql var with each iteration of the loop...

if that's ok, then just use the "." as a concat in your string

Code: Select all

$sql = "select first".$aaa."last from mytable where id = ".$aaa;