Jump to content

Variable within Variables for Querys


law

Recommended Posts

I'm sure this can be done but obviously I have the syntax wrong!

here is my fetch array command

$fieldq="SELECT * FROM fieldnames WHERE id = 1";
$fieldsql=mysql_query($fieldq);
$fieldrow = mysql_fetch_array($fieldsql);

//------------------Variables------------
$slot1=$fieldrow['slot1'];

 

I would like to generate a while () loop to produce something like

 

$num=1;
while ( $num < 30){
$slot.$num=$fieldrow['slot$num'];
$num++;
}

 

The above code doesn't work so obviously i am making a newbie mistake somewhere.. im sure this has been answered before but i didn't know what to search for to find my answer.. soo sorry in advance, Thanks

Link to comment
https://forums.phpfreaks.com/topic/94638-variable-within-variables-for-querys/
Share on other sites

Variables inside single quotes are not evaluated. You want to do something like this:

<?php
$num=1;
while ( $num < 30){
${slot.$num}=$fieldrow['slot' .$num];
$num++;
}?>

 

You may want to consider using an array here:

<?php
$slot = array();
for ($num=1;$num<30;$num++)
   $slot[$num] = $fieldrow['slot' . $num];
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.