php – 这个MySQL查询有什么问题.它给出了无效的参数编号错误

MySQL给出了这个错误:

SQLSTATE[HY093]: Invalid parameter number

对于此查询:

public function getUserFriends($userId) {
    $query = "SELECT users.id, users.firstName, users.lastName, users.thumbPic FROM friendships LEFT JOIN users ON ((friendships.user1 = users.id OR friendships.user2 = users.id) AND users.id <> :userId) WHERE (user1 = :userId OR user2 = :userId) AND friendships.status = 1";
    return $this->adapter->prepare($query)->execute(array(':userId' => $userId))->fetchAll();
}

参数号对我来说还不错,或者我错过了什么?

最佳答案 不幸的是,你不能两次使用相同的命名参数.你必须以这种方式实现你的目标:(我知道……跛脚,对吧?)

public function getUserFriends($userId) {
    $query = "
        SELECT
            users.id, users.firstName, users.lastName, users.thumbPic 
        FROM
            friendships 
        LEFT JOIN 
            users ON 
                (friendships.user1 = users.id OR friendships.user2 = users.id) 
                AND users.id <> :userIdA
        WHERE 
            (user1 = :userIdB OR user2 = :userIdC) 
            AND friendships.status = 1";
    return $this->adapter->prepare($query)->execute(array(
        ':userIdA' => $userId
        ':userIdB' => $userId
        ':userIdC' => $userId
    ))->fetchAll();
}
点赞