1

I need to insert same data to my MySQL table without having PHP loop. The reason why I'm doing this is that because I have a column with Auto_Increment feature and that column associates with other table.

So, I just need to insert some exactly same data and it's multiple rows (dynamic) but by using single INSERT syntax below :

INSERT INTO outbox_multipart (TextDecoded) VALUES ('$SMSMessage')

how to have this single INSERT syntax, but produce n number of rows?

1
  • how to do it? example please.. Commented Sep 27, 2012 at 15:30

5 Answers 5

2

You can do this:

INSERT INTO outbox_multipart (TextDecoded) VALUES ('$SMSMessage')
, ('$SMSMessage2'), ('$SMSMessage3'), ('$SMSMessage4');
Sign up to request clarification or add additional context in comments.

6 Comments

SMSMessage2 is a variable, not a string.
he mentioned its the same data i.e. a string in variable $SMSMessage
@coder1984 I agree with you, but the OP wasn't clear in this, he didn't say a variable. It was just the $SMSMessage in the statement he wrote. So I did what he asked fore which is how to have this single INSERT syntax, but produce n number of rows
if u dont consider it to be a variable, then you condsider it to be a string, which again would be '$SMSMessage', $ being considered as a char, as per his question. you changed it to SMSMessage
its just an observation, thats all :)
|
0
INSERT INTO outbox_multipart (TextDecoded) VALUES ('$SMSMessage'),('$SMSMessage')
,('$SMSMessage'),('$SMSMessage')

if done dynamically,

$n=5;
for ($i=0;$i<$n;$i++){$values.="('$SMSMessage'),";}
$values=substr($values,0,-1);

And the SQL be:

INSERT INTO outbox_multipart (TextDecoded) VALUES $values

Comments

0
mysql_query("INSERT INTO `table`(`this`) VALUES (`that`); INSERT INTO `table`(`this`) VALUES (`that`);");

Comments

0

try something like this

     $sql = mysql_query("INSERT INTO outbox_multipart (TextDecoded) VALUES ('$SMSMessage')
      , ('$SMSMessage'),('$SMSMessage'),('$SMSMessage')");

Also, just so you know, mysql_* have been deprecated. So, try to use mysqli_* or PDO for querying.

Comments

0

suppose you want 3 insertions simply do like this:

INSERT INTO outbox_multipart (TextDecoded) VALUES ('$SMSMessage'),('$SMSMessage'),('$SMSMessage');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.