PHP PDOException:“SQLSTATE[HY093]:无效的参数号"; [英] PHP PDOException: "SQLSTATE[HY093]: Invalid parameter number"
问题描述
当我尝试运行以下函数时收到错误SQLSTATE[HY093]: Invalid parameter number":
I'm getting the error "SQLSTATE[HY093]: Invalid parameter number" when I try to run the below function:
function add_persist($db, $user_id) {
$hash = md5("per11".$user_id."sist11".time());
$future = time()+(60*60*24*14);
$sql = "INSERT INTO persist (user_id, hash, expire) VALUES (:user_id, :hash, :expire) ON DUPLICATE KEY UPDATE hash=:hash";
$stm = $db->prepare($sql);
$stm->execute(array(":user_id" => $user_id, ":hash" => $hash, ":expire" => $future));
return $hash;
}
我觉得这很简单,我只是没有抓住.有什么想法吗?
I feel like it's something simple that I'm just not catching. Any ideas?
推荐答案
尝试:
$sql = "INSERT INTO persist (user_id, hash, expire)
VALUES (:user_id, :hash, :expire)
ON DUPLICATE KEY UPDATE hash=:hash2";
和
$stm->execute(
array(":user_id" => $user_id,
":hash" => $hash,
":expire" => $future,
":hash2" => $hash)
);
文档摘录(http://php.net/manual/en/pdo.prepare.php):
当您调用 PDOStatement::execute() 时,您必须为希望传递给语句的每个值包含一个唯一的参数标记.您不能在准备好的语句中两次使用同名的命名参数标记.不能将多个值绑定到单个命名参数,例如 SQL 语句的 IN() 子句.
You must include a unique parameter marker for each value you wish to pass in to the statement when you call PDOStatement::execute(). You cannot use a named parameter marker of the same name twice in a prepared statement. You cannot bind multiple values to a single named parameter in, for example, the IN() clause of an SQL statement.
这篇关于PHP PDOException:“SQLSTATE[HY093]:无效的参数号";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!