If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Search users

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-20-11, 16:49
Themooser Themooser is offline
Registered User
 
Join Date: Dec 2011
Posts: 1
Search users

Hi All,

I am making a kind of dating site. Users can add friends and here i have a problem.

When the user types a letter of the name they are searching for in the input box. There is a request made to my php file that returns 6 users that contain the letters of the user input.

I use this query to search for users:
Code:
 select id_user, username from users where username like '%".mysql_real_escape_string($find_user_name)."%' LIMIT 6
The problem is that the users that are already friends also are returned.

My database:
Table users: id_user, user_name, email, ..., ....
Table friends: users_id_user, friend_id

I thought about making a view of users and then removing the ids that are in my friend_id and then use the view to search for new friends. Is there a better way of doing this?

Thanks in advance!

Nick
Reply With Quote
  #2 (permalink)  
Old 12-20-11, 17:29
shureg shureg is offline
Registered User
 
Join Date: Oct 2011
Location: Hamburg, Germany
Posts: 15
you can join the friend table in your query
e.g.
Code:
select * 
from user u
left join friend f on f.user_id = u.user_id
where 
f. friend_id  is null   --so you can see when user has no friends
and username like '%".mysql_real_escape_string($find_user_name)."%' 
LIMIT 6
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On