Pdo按编号插入多行

时间:2015-02-23 21:52:31

标签: php pdo

我没有什么问题,我也不知道如何让它发挥作用。 我想进行按编号插入行的查询。 例: 如果发布的数字是8,则查询添加8行。

知道如何用pdo做到这一点吗?

1 个答案:

答案 0 :(得分:0)

SQL

INSERT INTO
    foo(bar,baz)
VALUES
    ("val1", "val2"),
    ("val1", "val2"),
    ("val1", "val2"),
    ("val1", "val2"),
    ("val1", "val2"),
    ("val1", "val2"),
    ("val1", "val2"),
    ("val1", "val2")
;

PHP

//Insert Statement
$sql = 'INSERT INTO foo(bar,baz) VALUES ';

//Values ("val1", "val2"),("val1", "val2"),("val1", "val2"),..
for ($i = 0; $i <= 8; $i++) {
    $sql.= '("val1", "val2"),';
}

//Remove last Comma, and set ";" at the end.
$sql = rtrim($sql, ",") . ';';