Forum Moderators: coopster

Message Too Old, No Replies

appending strings

         

bagheera

6:38 pm on Mar 16, 2004 (gmt 0)

10+ Year Member



How would I build up a string so that i get

$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...?

daisho

6:53 pm on Mar 16, 2004 (gmt 0)

10+ Year Member



Not really sure what you are asking. You want to dynamically create data? You want to insert a sequence of numbers into a table? If you want to insert a sequence of numbers try:

$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.

coopster

7:28 pm on Mar 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



daisho, I believe bagheera wants to INSERT 30 new rows in the table, each having the value of the same variable except an incremented number appended to it.

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);

globay

7:46 pm on Mar 16, 2004 (gmt 0)

10+ Year Member



I would use an array just like this:

$variable[$i]

bagheera

8:26 pm on Mar 16, 2004 (gmt 0)

10+ Year Member



I appreciate for the replies. I will try them out...
thanks