我已經構建了一個函數,它將準備SQL語句並使用給定的參數執行它。因此,這裏的樣子:PDO Mysql SQLSTATE [42000]
function go($statement) {
$q = self::$connection->prepare($statement, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
for($i = 1; $i < func_num_args(); $i++) {
$arg_to_pass = func_get_arg($i);
$q->bindParam($i, $arg_to_pass, PDO::PARAM_INT);
}
$q->execute();
}
但是,當我把它,它給了我下面的錯誤:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
然而,這兩種變型完美的工作:
function go($statement) {
$q = self::$connection->prepare($statement, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
for($i = 1; $i < func_num_args(); $i++) {
$q->bindValue($i, func_get_arg($i), PDO::PARAM_INT);
}
$q->execute();
}
(這個很笨,但只是爲了測試)
function go($statement) {
$q = self::$connection->prepare($statement, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$arg_to_pass = func_get_arg(1);
$q->bindParam(1, $arg_to_pass, PDO::PARAM_INT);
$arg_to_pass2 = func_get_arg(2);
$q->bindParam(2, $arg_to_pass2, PDO::PARAM_INT);
$q->execute();
}
那麼爲什麼bindParam不能在循環內工作?