Hello, I am using two searches. But niether will allow for search strings with more than one word.

Search.php

/* Only works using one word

if ($search) // performs search only if a search string was entered.
{

mysql_connect() or die ("Problem connecting to Database");

// Define a SQL Select Query

$sql= mysql_query("SELECT wname, title, keywords, description, city, country, category FROM directory WHERE city LIKE '%$search%' || wname LIKE '%$search%' || title LIKE '%$search%' || keywords LIKE '%$search%' || description LIKE '%$search%' || country LIKE '%$search%' || category LIKE '%$search%'", $db);

Search2.php

/* Does not work at all

if ($search) // performs search only if a search string was entered.
{

mysql_connect() or die ("Problem connecting to Database");

$terms = preg_split("/\s+/",$search);

$term = "%".$terms."%";

// Define a SQL Select Query

$sql= mysql_query("SELECT wname, title, keywords, description, city, country, category FROM directory
WHERE city LIKE '$term' || wname LIKE '$term' || title LIKE '$term' || keywords LIKE '$term' || description LIKE '$term' || country LIKE '$term' || category LIKE '$term'", $db);

Does anyone have any ideas how i can make my search form get results using more than one word?

~Shane

    Yes, with a full text search.

    Drop the shonky '||' syntax , it is not standard SQL and makes your code unportable even if mysql accepts it, other db will not. Just use OR when you mean OR, two key strokes as well.

    I take it you mean match exact phrase, because that is what you have coded. If it needs to match words in any order, or match any words, then you need to explode the string into a word array and itterate through it to build your query with LIKE for each word.

      I tried a FULL TEXT STRING but it did not work either:

      if ($search) // performs search only if a search string was entered.
      {

      mysql_connect() or die ("Problem connecting to Database");

      $terms="%".$search."%";
      $escaped_terms = mysql_escape_string($terms);

      // Define a SQL Select Query

      $sql= mysql_query("SELECT wname, url, title, keywords, description, city, country, category
      FROM directory MATCH (wname, url, title, keywords, description, city, country, category) AGAINST $escaped_terms", $db)


      This did not work either:

      *The result was a blank screen; however, single word search strings do work (not multi-word).

      if ($search) // performs search only if a search string was entered.
      {

      mysql_connect() or die ("Problem connecting to Database");

      // Strip White Space & Spaces *not working

      function wsstrip(&$search)
      {
      $search= preg_replace('/\s+/', ' ', $search);
      $search=ereg_replace (' +', ' ', trim($search));
      $search=ereg_replace("[\r\t\n]","",$search);
      }

      // Define a SQL Select Query

      $sql= mysql_query("SELECT wname, title, keywords, description, city, country, category FROM directory WHERE city LIKE '%%$search%%' OR wname LIKE '%%$search%%' OR title LIKE '%%$search%%' OR keywords LIKE '%%$search%%' OR description LIKE '%%$search%%' OR country LIKE '%%$search%%' OR category LIKE '%%$search%%'", $db);


      I am unable to use multi-word search strings.

      ~Shane

        Not a full text string but a full text search .

        You might also search these forums, I know that this problem came up before christmas so there is at least 1 relevent thread. Truth is there are probably 100 threads about multiple word searches, take your pick from the solutions on offer. But full text indexes and searching sounds like what you are actually after.

          Write a Reply...