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?

4

5 回答 5

2

你可以这样做:

INSERT INTO outbox_multipart (TextDecoded) VALUES ('$SMSMessage')
, ('$SMSMessage2'), ('$SMSMessage3'), ('$SMSMessage4');
于 2012-09-27T15:28:49.310 回答
0

尝试这样的事情

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

此外,正如您所知,mysql_*已被弃用。因此,尝试使用 mysqli_* 或 PDO 进行查询。

于 2012-09-27T15:30:06.693 回答
0
INSERT INTO outbox_multipart (TextDecoded) VALUES ('$SMSMessage'),('$SMSMessage')
,('$SMSMessage'),('$SMSMessage')

如果动态完成,

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

而 SQL 是:

INSERT INTO outbox_multipart (TextDecoded) VALUES $values
于 2012-09-27T15:28:33.597 回答
0

假设您想要 3 次插入,只需这样做:

INSERT INTO outbox_multipart (TextDecoded) VALUES ('$SMSMessage'),('$SMSMessage'),('$SMSMessage');
于 2014-10-27T09:34:23.797 回答
0
mysql_query("INSERT INTO `table`(`this`) VALUES (`that`); INSERT INTO `table`(`this`) VALUES (`that`);");
于 2012-09-27T15:37:35.550 回答