Page 1 of 1
php/mysql search engine problem
Posted: Fri Aug 08, 2003 12:52 pm
by GandalfTheBrown
i made a search, for my website, and i have a slight problem becuase i cant get it to search more then a single field.
$query = "select * from data where title like \"%$trimmed%\"
order by ID desc";
right now, it only searches the field named title, but i need it to search several more, about 13-15 in all.......how will i go baout doing this?
Posted: Fri Aug 08, 2003 3:10 pm
by skateis2s
$result = mysql_query ("SELECT * FROM data WHERE title LIKE '%$trimmed%' ORDER BY id DESC");
its basically the same, might as well give it a whirl?
Posted: Fri Aug 08, 2003 7:20 pm
by McGruff
The SELECT part of the mysql query string only specifies which columns to return. The WHERE clause specifies where to look.
So you need to add a bunch of WHERE's and OR's:
Code: Select all
<?php
$query = "SELECT * FROM data WHERE title LIKE '%" . $trimmed . "%' OR col2 LIKE '%" . $trimmed . "%' OR col3 LIKE '%" . $trimmed . "%' OR ..etc"
echo $query; #debug line - helps to check syntax - remove later
?>
This doesn't identify which column was matched in any particular result row - you might not need to do that.
LIKE is a "loose" search which returns suffixes and prefixes - $trimmed = 'buff'; would also return buffy, rebuffed etc. An RLIKE search cuts these out - see mysql manual, mysql.com, or post again if you can't find that bit (the manual search leaves a lot to be desired, at least in the version I've got).