Re: Multiple MySQL INSERT into loop
| From: | Jim Lucas | Date: | Thu, 07 Feb 2008 18:11:22 +0000 |
| Subject: | Re: Multiple MySQL INSERT into loop | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-268891@lists.php.net to get a copy of this message | ||
Rob G wrote:
I'm working on this, and am not sure how the variable syntax needs to be here. This is what I have done manually. //BEGIN EXAMPLE mysql_query ("INSERT INTO testimonials (id, quote, name) VALUES ('$id_1', '$entry_1', '$name_1')"); mysql_query ("INSERT INTO testimonials (id, quote, name) VALUES ('$id_2', '$entry_2', '$name_2')"); mysql_query ("INSERT INTO testimonials (id, quote, name) VALUES ('$id_3', '$entry_3', '$name_3')"); //BEGIN EXAMPLE while ($i<16) { $i++; mysql_query ("INSERT INTO testimonials (id,quote,name) VALUES ('$id_1', '$entry_1', '$name_1')");)");You are looking for variable variables http://us3.php.net/manual/en/language.variables.variable.php example of what is going on. <?php # Variable Variables Test $id_0 = 'zero'; $id_1 = 'one'; $id_2 = 'two'; $id_3 = 'three'; for ( $i=0; $i<4; $i++ ) { echo ${'id_'.$i}; } ?> So, for what you are wanting to do. I would do something like this. <?php # # Your other code here # for ( $i=1; $i<=16; $i++ ) { $SQL = "INSERT INTO testimonials (id,quote,name)
VALUES (
'".${'id_'.$i}."',
'".${'entry_'.$i}."',
'".${'name_'.$i}."'
)
";
echo $SQL;
# mysql_query ( $SQL );} ?>
} //END EXAMPLE What I'm trying to figure out, is exactly how I need to format my entry within the VALUES section, so that it will change based on the value of $i.-- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare