1,105,208 Community Members

User Search

Member Avatar
thecameraman
Newbie Poster
15 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I am trying to create at user search for my website and I'm not sure where to start.

My table name is users with user_id, user_name, name

I'd like to be to search and have a page come up with the top 5 closest results.

Sorry if this question has been asked and that I have no code.

Member Avatar
pritaeas
mod_pritaeas
11,296 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,832 [?]
Skill Endorsements: 155 [?]
Moderator
Featured
Sponsor
 
0
 

You can use a SELECT query using LIKE and LIMIT. Start here:

http://php.net/mysql_query
http://dev.mysql.com/doc/refman/5.6/en/select.html

Member Avatar
thecameraman
Newbie Poster
15 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks I will check those sites out. I make videos and I am a graphic designer for my company so they automatically assume I am a web programmer.

Member Avatar
thecameraman
Newbie Poster
15 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

It doesn't seem to like it. One of the links says to say

"SELECT * FROM users LIKE %'John'%"

But I get an error and the query doesn't run when I have % in there.

Member Avatar
pritaeas
mod_pritaeas
11,296 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,832 [?]
Skill Endorsements: 155 [?]
Moderator
Featured
Sponsor
 
0
 

Put the percentage signs inside the single quotes.

Member Avatar
diafol
Where are my eyes?
12,953 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,846 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 

Using LIKE has always bugged the hell out of me, as it doesn't take relevancy into account (e.g. exact matches before partials). I did a bit of searching for 'fulltext indexing'. I found this thread:

http://stackoverflow.com/questions/1016804/how-to-sort-mysql-fulltext-search-results-by-relevancy

From which the following example has been cobbled:

ALTER TABLE `users` ADD FULLTEXT INDEX `SEARCH`(`username`);

SELECT *, MATCH(username) AGAINST ('John' IN BOOLEAN MODE) AS relevance 
FROM `users`
WHERE MATCH(username) AGAINST ('John' IN BOOLEAN MODE)
ORDER BY relevance DESC

I *think* you can use multiple fields for searching in the MATCH list too.

Member Avatar
pritaeas
mod_pritaeas
11,296 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,832 [?]
Skill Endorsements: 155 [?]
Moderator
Featured
Sponsor
 
1
 

Note that you have to have MyISAM tables, to use full-text search.

Member Avatar
thecameraman
Newbie Poster
15 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thank you guys!

Question Answered as of 2 Years Ago by pritaeas and diafol
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: