这很好用:
...
$sql = "INSERT INTO person (`name`, `age`)
VALUES ('John', 34);";
$stmt = $conn->prepare($sql);
...
但这会产生错误:
...
$sql = "BEGIN
INSERT INTO person (`name`, `age`)
VALUES ('John', 34);
END;";
$stmt = $conn->prepare($sql);
...
错误是:
SyntaxError: unterminated string literal
更具体地说:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO person (`name`, `age`)
我知道在这个人为的例子中,BEGIN ... END是不必要的,但为什么它不起作用?
非常感谢您的帮助。
答案 0 :(得分:1)
在PDO中,与任何其他API一样,使用单独的API调用运行单独的查询:
鉴于END关键字没有意义,假设您的意思是COMMIT,
$conn->query("BEGIN");
$conn->query("INSERT INTO person (`name`, `age`) VALUES ('John', 34)");
$conn->query("COMMIT");
但是,你必须记住