Skip to main content
Inspiring
June 25, 2010
Answered

mysql two table help

  • June 25, 2010
  • 1 reply
  • 527 views

I need to search  for a user and bring his details up only if he is in another user's  friends list.

I have a user table with user_id, user_fname,  user_sname etc.. and a friends table with friend_id, ur_id, their_id,  approved

I have a ajax called php script to auto populate based  on key pressing the letters that match the query.

Here is the  query:

SELECT user_id, user_fname, user_sname, user_city, photo,  email FROM users, friends WHERE ur_id = '$_SESSION[user_id]' AND  user_fname LIKE '%$queryString2%' OR user_sname LIKE '%$queryString2%'

I  seem to get all values, and doubles when I use this method, I dont  think I can use a left join as I cant put the user_id and ur_id over  each other.

Please can someone help me with this?

Thank  you.

This topic has been closed for replies.
Correct answer

Yes I saw that, but that only checks one table for a certain value (value of $_SESSION["user_id"]).

But when you pull data from second table, you need to match a common value between tables, which is user_id in your case.

You will pull the data from 2nd table ONLY if 2nd table's user_id is same with the 1st tables user_id.

1 reply

June 25, 2010

If I get it right, you will need one more condition on your WHERE clause:

ur_id = user_id

This way mysql will only pull data with same user ids

Inspiring
June 28, 2010

hi,

I already have ur_id =  '$_SESSION[user_id]'

Correct answer
June 28, 2010

Yes I saw that, but that only checks one table for a certain value (value of $_SESSION["user_id"]).

But when you pull data from second table, you need to match a common value between tables, which is user_id in your case.

You will pull the data from 2nd table ONLY if 2nd table's user_id is same with the 1st tables user_id.