問題描述
有什么辦法可以將這些 bindParam 語句放入一個語句中嗎?
Is there a way I can put these bindParam statements into one statement?
$q = $dbc -> prepare("INSERT INTO accounts (username, email, password) VALUES (:username, :email, :password)");
$q -> bindParam(':username', $_POST['username']);
$q -> bindParam(':email', $_POST['email']);
$q -> bindParam(':password', $_POST['password']);
$q -> execute();
我在可能的情況下使用之前準備好的 mysqli,我切換到 PDO 以獲得 assoc_array 支持.在 PDO 的 php.net 網站上,它在單獨的行上顯示它們,在我看到的所有示例中,它都在單獨的行上.
I was using mysqli prepared before where it was possible, I switched to PDO for assoc_array support. On the php.net website for PDO it shows them on seperate lines, and in all examples I have seen it is on seperate lines.
有可能嗎?
推薦答案
上的示例 2execute
頁面就是你想要的:
Example 2 on the execute
page is what you want:
$sth->execute(array(':calories' => $calories, ':colour' => $colour));
您可能還想查看其他示例.使用問號參數,它將是:
You may want to look at the other examples too. With question mark parameters, it would be:
$q = $dbc -> prepare("INSERT INTO accounts (username, email, password) VALUES (?, ?, ?)");
$q->execute(array($_POST['username'], $_POST['email'], $_POST['password']));
如果只有這些列,您可以只寫:
If those are the only columns, you can just write:
$q = $dbc -> prepare("INSERT INTO accounts VALUES (?, ?, ?)");
$q->execute(array($_POST['username'], $_POST['email'], $_POST['password']));
這篇關于PDO bindParam 成一個語句?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!