4

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

参数编号对我来说看起来不错还是我错过了什么?

4

1 回答 1

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();
}
于 2013-04-05T21:46:30.373 回答