frequently ask ? : Computers : Programming : Languages : PHP : Database Backed Sites : MySQL

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

9 of 15 people (60%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

A search for 'charlie brown' using two fields returns results for 'charlie brown','charlie' and 'brown' but how can I prioritise these results?

Jun 10th, 2001 21:14
Michael Nolan, Dave Martindale,


Firstly, you need to set the fields you're searching to be FULL TEXT 
indexed using the alter table statement.  Then you can do a search on 
the table:
$search_result = mysql_query("SELECT id,concat(surname,', ',firstname) 
AS fullname, MATCH (firstname,surname) AGAINST ('" . 
		$sq . 
		"') AS score FROM personal WHERE MATCH 
(firstname,surname) AGAINST ('" . 
		$sq . 
		"') ORDER BY score DESC;");
$sq is the search query.  The where clause in this case makes sure that 
only results which match something are returned.  The score column will 
contain a float number representing how accurate the match is.  Sort by 
score and Bob's your Uncle.