0

我有以下工作正常的连接,但我想将表中的 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]:无效的参数号:绑定变量的数量与令牌的数量不匹配

问题可能出在哪里?

4

1 回答 1

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

注意:如果不是更好地摆脱它,绑定变量应该始终存在于您的查询中。

于 2013-10-02T02:38:55.627 回答