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

时间:2013-04-05 18:14:48

标签: php mysql pdo

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();
}

参数号对我来说没问题,或者我错过了什么?

1 个答案:

答案 0 :(得分:2)

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

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();
}