tags:

views:

21

answers:

2

How can I query my database to check if user_id or friend_id are equal to the logged in users ID and that their friendship_status has been accepted.

Here is the MySQL code I got so far.

SELECT COUNT(id) as num 
FROM users_friends 
WHERE users_friends.user_id = '$user_id' 
AND friendship_status = '1'

Here is my MySQL table.

CREATE TABLE users_friends (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED UNSIGNED NOT NULL,
friend_id INT UNSIGNED NOT NULL,
friendship_status TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
date_created DATETIME NOT NULL,
);
A: 
SELECT COUNT(id) as num 
FROM users_friends 
WHERE (
    user_id = '$sanitized_user_id' 
    OR friend_id = '$sanitized_user_id'
  )
  AND friendship_status = '1'
Ignacio Vazquez-Abrams
This does not work :(
favor
We can only give accurate answers to accurate questions.
Ignacio Vazquez-Abrams
Sorry, No need to get texty I thought my answer was clear so I made it clearer:)
favor
A: 
SELECT    id
FROM      user_friends
WHERE     (user_id = '$logged_in_user_id'
          OR friend_id = '$logged_in_user_id')
          AND friendship_status = 1

If the user_id or friend_id is equal to the logged in uses id and there is a friendship, the returned value will contain corresponding id. If it is not found, then the returned value will be null, which can be checked easily(in MySQL there is probably something like ISNULL(value_to_be_checked) for this).

You must sanitize the logged in user id properly before using it in a query.

Night Shade