arboles
arboles

Reputation: 1331

mysql query to return users who are following me is returning this error: Undefined property: stdClass::$user_id

This code is for my follower system. I want to query the database to show the users who are following me. the function show_users_following_you(my id) is returning an error on line 9. can someone explain where it is coming from?

This code is for my follower system. I want to query the database to show the users who are following me. the function show_users_following_you(my id) is returning an error. can someone explain where it is coming from?

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;

}

Comments Table structure is

Two columns:

User_id corresponds to who is being followed and Follower_id corresponds to who is doing the following.

Upvotes: 0

Views: 118

Answers (3)

Pete
Pete

Reputation: 635

Please correct the query

"select follower_id from following where user_id='$user_id'"

To

"select user_id from following where user_id='". $user_id ."'"

Upvotes: 0

Lobo
Lobo

Reputation: 4157

Instead of

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

Try

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

Regards!

Upvotes: 0

user1233508
user1233508

Reputation:

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

Your fetched object has the property follower_id, but you're trying to read user_id.

(Also, make sure to escape dynamic values (in this case, $user_id) using mysql_real_escape_string or PDO. As it stands, this is potentially open to SQL injection.)

Upvotes: 2

Related Questions