How do I set up a search for of database searching for 2 words in different orders

When will i search a database one example is, if someone applies in " shed foot", it would go back any instance belonging to the word foot considering the word loose. When they search loose feet, and foot loose is the database, may well show up within the search.

Exactly where Field like ”%foot%’ and Field Like ‘%loose%’

The exact formatting within your where clause would depend your database, but this provides you with the general concept. You’d have to split your query having a server-side programming dialect, or in possible of SQL Server the easily available Split function, however it will work.

I am just using php & MySQL

I wouldn’t really be capable of help you rather than in concept, and then. You’d have to look for a PHP split function most likely and then file format your query keeping that in mind for MySQL.

Ideally you’d use like a Lucene index for this kind of thing. However as TheGAME1264 suggests you possibly can perform searches similar to this with MySQL. Though this will have to match every word

Something like this would work: (Not completely tested)

Just put in a query stringed like s=this+is+a+test towards the page

PHP:

< php

/*******************************************
Example  Class  to  create  loose  keyword  style
searched  with  MySQL.
*******************************************/

class  Keyword_Search_Model 

private  $search;
private 
$fieldName;
private 
$tableName;
private 
$queryConditions;

public  function  __construct() 
$this-> search  $this-> findSearchTerms();

private  function  createQueryConditions() 
if  (isset(
$this-> fieldName)  & &   $this-> fieldName  ! =  NULL
$query_words  explode("  ",   $this-> search);   //Explode  into  array  by  space
$array_length  count($query_words);   //Get  count  of  array

$condition  "WHERE  ".   $this-> fieldName.   "  LIKE  ";
$i  0;
foreach(
$query_words  as  $word
$condition  . =  "'%".   $word.   "%'  ";
if(
$array_length  ! =  ($i  1))    //If  it's  not  the  last  iteration  of  the  array
$condition. =  "AND  ".   $this-> fieldName.   "  LIKE  ";

$i++;

$this-> queryConditions  $condition;

else 
throw  new  Exception('Field  Name  Not  Arranged. ');

private  function  <span style="col

This entry was posted in Web Design and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *