-2

I have this in my BD:

following: 12,13,2,4 (this is the user id of the following list of one user)

and I want to count how many followers has a single user.

I have this code:

  $followers = $db->fetch_field($db->simple_select('users', 'COUNT(*) AS followers' , "buddylist IN ($useruid) "), 'followers');
  $Followers = $followers;

This only works if the first number of the list is the same that $useruid. Example:

$useruid = 1, buddylist = 3,2,1

$followers = $db->fetch_field($db->simple_select('users', 'COUNT(*) AS followers' , "buddylist IN ($useruid) "), 'followers');
      $Followers = $followers;

the out put its 0

if buddylist is 1,2,3

the output is one.

What's wrong? :(

2 Answers2

0

You could use FIND_IN_SET() instead of IN(), since IN will look for the string you compare in a group of structured fields, whereas your field is a comma separated string of IDs. FIND_IN_SET() will look within that string:

$followers = $db->fetch_field(
    $db->simple_select(
        'users',
        'COUNT(*) AS followers',
        "FIND_IN_SET($useruid, buddylist)"
    ),
    'followers'
);

See this related question.

Community
  • 1
  • 1
scrowler
  • 24,273
  • 9
  • 60
  • 92
-1

It is hard to tell not knowing your DB contents.

I suppose you have users in $useruid as array

convert your array to string by `implode(',',$useruid)` and substitute $useruid before mysql statment

and all should work fine

bart
  • 1
  • 2