使用绑定值与PDO连接不起作用

时间:2013-10-02 02:28:53

标签: php

我有以下连接工作正常,但我想在表中包含新变量中的userID列:

public function userLogin() 
{
    $success = false;
    try {
        $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
        $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        $sql = "SELECT * FROM users WHERE username = :username AND password = :password LIMIT 1";
        $stmt = $con->prepare( $sql );
        $stmt->bindValue( "username", $this->username, PDO::PARAM_STR );
        $stmt->bindValue( "password", hash("sha256", $this->password . $this->salt), PDO::PARAM_STR );
        $stmt->bindValue( "UserID", $this->userID, PDO::PARAM_STR );

        $stmt->execute();

        $valid = $stmt->fetchColumn();

        if( $valid ) {
            $success = true;
        }

        $con = null;
        return $success;

当我添加新行$stmt->bindValue( "UserID", $this->userID, PDO::PARAM_STR );

它说错误:SQLSTATE [HY093]:参数号无效:绑定变量数与令牌数不匹配

哪里可能是问题?

1 个答案:

答案 0 :(得分:1)

您的绑定3值但仅使用2值

$sql = "SELECT * FROM users WHERE username = :username AND password = :password LIMIT 1";

如果您要使用ID添加它。

$sql = "SELECT * FROM users WHERE userid= :UserID AND username = :username AND password = :password LIMIT 1";

注意:如果不能更好地删除它,那么绑定变量应始终存在于您的查询中。