Scripts and functions that I have written or found useful

PHP + MySQL Recursive Word Search

September 8th, 2006 Posted in MYSQL, PHP

This script will allow you totype asmany words as you like into a text box and find instances where all of these words exists in any field. The words entered only need to be seperated by spaces, no commas required!

PHP:
  1. $search = explode(" ",$keywords);
  2. $count = count($search);
  3. for($x=0;$x<$count;$x++){
  4. if($repeat == 0){
  5. $sqlquery .= " WHERE (keyword1 LIKE '%".$search[$x]."%' OR keyword2 LIKE '%".$search[$x]."%' OR caption LIKE '%".$search[$x]."%')";
  6. $repeat = 1;
  7. }else{
  8. $sqlquery .= " AND (keyword1 LIKE '%".$search[$x]."%' OR keyword2 LIKE '%".$search[$x]."%' OR caption LIKE '%".$search[$x]."%')";
  9. }
  10. }
  11. $sql = mysql_query("SELECT * FROM library ".$sqlquery) or die(mysql_error());

Post a Comment