MySQL WHERE 4 of the 10 keywords available in records

Discussion in 'MySQL' started by gilgalbiblewheel, Jun 24, 2008.

  1. #1
    Is it possible to have several keywords in the search separated with an OR but only to show the records where there are ...let's say 4 of the 10 keywords available?
     
    gilgalbiblewheel, Jun 24, 2008 IP
  2. Riverofrhyme

    Riverofrhyme Peon

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do you mean like:

    $query = mysql_query("SELECT * FROM table WHERE field1='$one' OR field2='$two' OR field3='$three'");

    That would work.
     
    Riverofrhyme, Jun 24, 2008 IP
  3. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    Like this:
    $sql="SELECT * FROM bible WHERE 1=1 AND";
    $j=0;
    //to sort out all words with length less than 4 like AND, OR, BUT...
    for ($i=0; $i < count($searchTheseArr); $i++){
    	if(strlen($searchTheseArr[$i]) > 4){
    		$newSearchTheseArr[$j] = $searchTheseArr[$i];
    //j renumbers the words of length 4 and up		
    		$j++;
    	}
    }
    //sql searches only length 4 and up
    for ($j=0; $j < count($newSearchTheseArr); $j++){
    	$sql.=" text_data LIKE '%" .$newSearchTheseArr[$j]. "%'";
    	if($j!=count($newSearchTheseArr)-1){
    		$sql.=" OR";
    	}else{
    //removes the OR from the last line and replaces with the following	
    		$sql.= " LIMIT " . $start . ", " . $perpage;
    	}
    }
    PHP:
    It's only one field but several keywords in this case called:
    $newSearchTheseArr[$j]
     
    gilgalbiblewheel, Jun 24, 2008 IP