0

此代码适用于我的追随者系统。我想查询数据库以显示关注我的用户。函数 show_users_following_you(my id) 在第 9 行返回错误。有人可以解释它的来源吗?

此代码适用于我的追随者系统。我想查询数据库以显示关注我的用户。函数 show_users_following_you(my id) 返回错误。有人可以解释它来自哪里吗?

function show_users_following_you($user_id=0){

    if ($user_id > 0){

        $follow = array();

        $fsql = "select follower_id from following where user_id='". $user_id ."'";
        $fresult = mysql_query($fsql);

        while($f = mysql_fetch_object($fresult)){
            array_push($follow, $f->user_id);
        }

        if (count($follow)){
            $id_string = implode(',', $follow);
            $extra =  " and id in ($id_string)";
        }else{
            return array();
        }
    }

    $users = array();
    $sql = "select id, username from users where status='active' $extra order by username";
    $result = mysql_query($sql);

    while ($data = mysql_fetch_object($result)){

        $users[$data->id] = $data->username;

    }

    return $users;

}

评论表结构是

两列:

user_id 对应谁被关注, Follower_id 对应谁在做关注。

4

3 回答 3

2
$fsql = "select follower_id from following
    where user_id='$user_id'";
$fresult = mysql_query($fsql);

while($f = mysql_fetch_object($fresult)){
     array_push($follow, $f->user_id);
}

您获取的对象具有属性follower_id,但您正在尝试读取user_id

(此外,请确保$user_id使用mysql_real_escape_string或 PDO 转义动态值(在本例中为 )。就目前而言,这可能对 SQL 注入开放。)

于 2012-04-09T10:48:07.893 回答
0

代替

[...]
while($f = mysql_fetch_object($fresult)){
   array_push($follow, $f->user_id);
}

尝试

[...]
while($f = mysql_fetch_object($fresult)){
   array_push($follow, $f->User_id);
}

问候!

于 2012-04-09T10:48:53.730 回答
0

请更正查询

“从 user_id='$user_id' 的关注中选择 follower_id”

“从下面的 user_id=' 中选择 user_id”。$user_id ."'"

于 2012-04-09T10:49:45.403 回答