![](/img/trans.png)
[英]How does PHP PDO's prepared statements prevent sql injection? What are other benefits of using PDO? Does using PDO reduce efficiency?
[英]How do you format BEGIN ... END statements using PDO's with SQL?
這工作正常:
...
$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 是不必要的,但為什么它不起作用?
非常感謝您的幫助。
在 PDO 中,就像在任何其他 API 中一樣,您使用單獨的 API 調用運行單獨的查詢:
鑒於 END 關鍵字沒有意義並且假設您的意思是 COMMIT,
$conn->query("BEGIN");
$conn->query("INSERT INTO person (`name`, `age`) VALUES ('John', 34)");
$conn->query("COMMIT");
但是,您必須記住
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.