Forum Moderators: coopster
$variable1 dynamically which should be used in a sql query like:
$sql = mysql_query("INSERT INTO **** (var) VALUES('$variable1');
the problem is that i need to run this 30 times and therefore need to create the $variable1, $variable2 etc on the fly - my problem is I cant get it working with my strings. Im using $i as integer in a loop. Tried things like
'$variable.$i'
this wont work since i get the value .1 inserted.
So how can I get this working...?
$count=30;
for($i=1;$i<=$count;$i++) {
mysql_query("INSERT INTO counter_table (var) VALUES('$i')" );
}
that will give you 1 to 30 in the table.
daisho.
bagheera, a period in between two single quotation marks is just a plain old period, it won't perform concatenation. If you want to concatenate, you'll have to use a bit different syntax when building your sql statement. Also, I would build a multiple values insertion list so that you only have to execute the query once, instead of thirty times:
$values_list = ''; // initialize
for ($i = 1; $i <= 30; $i++) {
$values_list .= "('" . $variable . $i . "'), ";
}
$values_list = rtrim($values_list, ', ');
$sql = "INSERT INTO **** (var) VALUES" . $values_list;
// exit($sql); // uncomment to halt processing and see the statement
mysql_query($sql);