gpt4 book ai didi

PHP PDO异常 : "SQLSTATE[HY093]: Invalid parameter number"

转载 作者:行者123 更新时间:2023-11-29 20:21:55 25 4
gpt4 key购买 nike

当我尝试运行以下函数时,收到错误“SQLSTATE[HY093]:无效参数号”:

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;
}

我觉得这很简单,我只是没有理解。有什么想法吗?

最佳答案

尝试:

$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 ):

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 PDO异常 : "SQLSTATE[HY093]: Invalid parameter number",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39469212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com