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

标签 php mysql pdo

当我尝试运行以下函数时,收到错误“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/24855480/

相关文章:

php - 获取MySQL中两个日期相差的天数

php - SQL限制返回所有结果

mysql - PDO 查询中的多个 mySQL 连接

mysql - PDO BindValue 不起作用,但适用于直接粘贴

php - 在 Laravel 中将信息添加到数据库而不丢失旧信息

php - "Add [name] to fillable property to allow mass assignment on [Illuminate\Foundation\Auth\User]."

循环中的 PHP POST 选择框未按正确顺序显示值

php - 在 Symfony 中创建一个 Cache Provider 类

php - 将Mysql结果集转换为JSON会打印相同字段两次

mysql - 在 JDBC MySql 连接字符串中添加 2 个 session 变量