Blog Search

Latest Posts

Recent Work: Digital Marketing Material and Website Re-Designs

We are now VAT Registered

Affordable Web Hosting and Domain Name Registration

New Acuras Logo Coming Soon...

Barclaycard ePDQ CPI Integrations

HSBC XML API / CPI E-commerce Integrations

Photoshop to Valid XHTML/CSS Coding Service

Recommended Open Source Invoicing System

Reasons Businesses should Embrace the Web

Henley Hearing new pages launched

Blog Archive

Complete Blog Archive

Browse by Subject

AJAX
Apache
CMS
CSS
HTML
Java
JavaScript
Links
Linux
Mac
MySQL
Pascal
PHP
postgreSQL
RSS
Ruby
SEO
Web Dev News
Windows
XML

Other Resources

Matt's Web Development Blog

Computer Science Student Articles

Affiliate Program Articles

PHP: Multi-word MySQL Search Algorithm and Output

Here we have a simple PHP search algorithm that queries a MySQL database and outputs the result. It is a simple alternative to using the FULLTEXT search feature of MySQL.

The script autmatically takes into consideration how many search words are in the search string which is submitted from the HTML search form. It then creates a MySQL query and executes it, displaying the results. You can easily edit the fields that the search script searches by changing the values in the $arrayFields array.

<?php
// checks if a search has been submitted
if(!empty($_REQUEST['search']))
{
  // the table to search
  $table = "yourTable";
  // explode search words into an array
  $arraySearch = explode(" ", $search);
  // table fields to search
  $arrayFields = array(0 => "title", 1 => "content");
  $countSearch = count($arraySearch);
  $a = 0;
  $b = 0;
  $query = "SELECT * FROM ".$table." WHERE (";
  $countFields = count($arrayFields);
  while ($a < $countFields)
  {
    while ($b < $countSearch)
    {
      $query = $query."$arrayFields[$a] LIKE '%$arraySearch[$b]%'";
      $b++;
      if ($b < $countSearch)
      {
        $query = $query." AND ";
      }
    }
    $b = 0;
    $a++;
    if ($a < $countFields)
    {
      $query = $query.") OR (";
    }
  }
  $query = $query.")";
  $query_result = mysql_query($query);
  // print title
  echo '<h1>Your Search Results</h1>'."\n\n";
  if(mysql_num_rows($query_result) < 1)
  {
    echo '<p>No matches found for "'.$search.'"</p>';
  }
  else
  {
    echo '<p>Search Results for "'.$search.'":</p>'."\n\n";
    // output list of articles
    while($row = mysql_fetch_assoc($query_result))
    {
      // output whatever you want here for each search result
      echo '<a href="index.php?id='.$row['id'].'">'.$row['title'].'</a><br />';
    }
  }
}
else
{
  // display a welcome page
}
?>

<p><form method="get">
  <input type="text" name="search" value="<?php echo $_REQUEST['search'] ?>" />
  <input type="submit" value="Search" />
</form></p>

This PHP script produces and executes the following MySQL query when given a search input of "really cool php scripts". "title" and "content" are the two fields that are being searched upon. 

SELECT * FROM articles WHERE (title LIKE '%really%' AND title LIKE '%cool%' AND title LIKE '%php%' AND title LIKE '%scripts%') OR (content LIKE '%really%' AND content LIKE '%cool%' AND content LIKE '%php%' AND content LIKE '%scripts%')

Subscribe to RSS Feed Bookmark and Share

Related Links

Related Articles / Posts

Recommended Open Source Invoicing System (25/03/2009)

Recent Recommended Links - October 2008 (03/10/2008)

Recent Recommended Links - April 2008 (11/04/2008)

Recent Recommended Links - March 2008 (16/03/2008)

Tutorial: Importing Excel Data to MySQL using phpMyAdmin (11/05/2007)

 
Acuras Web Development Paje House 164 West Wycombe Road High Wycombe HP12 3AE United Kingdom 08712 887 565
Tel: 08712 887 565
Fax: 08712 887 566
Email: info@acuras.co.uk
Skype: acuras_web
Paje House
164 West Wycombe Road
High Wycombe
Buckinghamshire
HP12 3AE