MySQL中的多查询支持?

时间:2013-06-24 06:30:20

标签: php mysql syntax

我在网上搜索了很长时间但没找到解决办法。我正在尝试插入表中,获取记录的ID并在另一个表中插入x次。 我的代码是

$first = true;
$sql  = 'INSERT INTO
`pf_employees` (firstname, surname, `occupationGroup`, birthday, image)
VALUES ("'.$name.'", NULL, NULL, NULL, NULL)';
$sql .= 'INSERT INTO `pf_roster_employees` (weekID, dayID, employeeID) VALUES ';
foreach($days as $temp) {
    if(!$first)
        $sql .= ', ';
    $sql .= '("'.$weekID.'", "'.$temp.'", last_insert_id())';
    $first = false;
}
$result = $config['database']->query($sql);

这样会产生以下结果: [...] INSERT INTO pf_roster_employees (weekID, dayID, employeeID) VALUES ("1", "1", last_insert_id()), ("1", "4", last_insert_id()), ("1", "5", last_insert_id())

口译员说You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO pf_roster_employees (weekID, dayID, employeeID) VALUES ("1", "1", ' at line 1

有没有更方便的方法来复制第二个查询?我的意思是,SQL比PHP快得多,当然,我对使用这些脏代码感到非常惭愧。

谢谢。

1 个答案:

答案 0 :(得分:0)

你忘记;这里是为了分离sql查询

 NULL, NULL, NULL)';
                 ^

NULL, NULL, NULL);';

同样使用mysqli_*,它支持在一个sigle语句中运行多个查询。