PHP PDOException:"SQLSTATE [HY093]:无效的参数编号" [英] PHP PDOException: "SQLSTATE[HY093]: Invalid parameter number"

查看:82
本文介绍了PHP PDOException:"SQLSTATE [HY093]:无效的参数编号"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行以下函数时,出现错误"SQLSTATE [HY093]:无效的参数编号":

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/zh/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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆