MySQL
  Home arrow MySQL arrow Page 4 - Completing a Search Engine with MySQL and PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
MYSQL

Completing a Search Engine with MySQL and PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 10
    2007-08-13


    Table of Contents:
  • Completing a Search Engine with MySQL and PHP 5
  • Listing the full source code of the original search application
  • Defining a simple web page generating class
  • Developing a fully-functional practical example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Completing a Search Engine with MySQL and PHP 5 - Developing a fully-functional practical example
    ( Page 4 of 4 )

    In consonance with the concepts deployed in the previous section, I'm going to set up an example where all the PHP classes defined earlier will be used in conjunction to make this search engine work as expected.

    In this case, I'm going to use a simple "USERS" MySQL database table, populated with some basic records to illustrate the functionality of the search engine in question. It's quite possible, however, that in a real situation you'll need to perform the pertinent search queries against multiple tables.

    Okay, assuming that the aforementioned "USERS" table is the same one that I used in the first article of the series, it is filled in with the following data:

    Id 

    firstname

    lastname

    email

    comments

     1

    Alejandro

    Gervasio

    alejandro@domain.com

    MySQL is great for building a search engine

     2

    John

    Williams

    john@domain.com

    PHP is a server side scripting language

     3

    Susan

    Norton

    sue@domain.com

    JavaScript is good to manipulate documents

     4

    Julie

    Wilson

    julie@domain.com

    MySQL is the best open source database server

    Here's a basic example that demonstrates how this extensible search application functions:

    try{
       // include classes
       require_once 'sessionhandler.php';
       require_once 'mysql.php';
       require_once 'webpage.php';
       // create new session handler object
       $sh=new SessionHandler();
       // connect to MySQL
       $db=new MySQL(array('host'=>'host','user'=>'user',
    'password'=>'password','database'=>'database'));
       // create new web page object
       $wp=new WebPage();
       // check if search term has been saved to session variable
       if(!$sh->getVariable('searchterm')){
         $searchterm=$db->escapeString($_GET['searchterm']);
         $sh->setVariable($_GET['searchterm'],'searchterm');
       }
       else{
         // get search term from session variable
         $searchterm=$sh->getVariable('searchterm');
       }
       // display header
       echo $wp->displayHeader();
       $result=$db->query("SELECT firstname, lastname,comments FROM
    users WHERE MATCH(firstname,lastname,comments) AGAINST
    ('$searchterm' IN BOOLEAN MODE)");
       if(!$result->countRows()){
         echo $wp->displayBody('<div class="maincontainer"><h2>No
    results were found. Go back and try a new search.</h2></div>');
         }
       else{
         // display search results
         echo $wp->displayBody('<div class="maincontainer"><h2>Your
    search criteria returned '.$result->countRows().'
    results</h2>'.$result->fetchPagedRows($_GET['page']).'</div>');
       }
       // display footer
       echo $wp->displayFooter();
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    As shown above, putting the search engine to work is a fairly easy process. It requires only using all the classes that were previously defined, in addition to performing the respective search queries by taking advantage of the full-text and Boolean capabilities offered by MySQL.

    Finally, take a look at the following screen shots. They show the different database results returned by the search engine, according to certain search strings entered in the corresponding web form:

    (results returned by entering the search string "Alejandro")

    (results returned by entering the search string "Alejandro+Susan")

    (results returned by entering the search string "Alejandro+Susan+John")

    As you can see in the previous images, building a search engine using the powerful MySQL/PHP 5 combination is indeed a no-brainer process that can be tackled with minor hassles. At this point, you have at your disposal all the required source files to incorporate this application into your own web site, and provide users with a simple mechanism to search and find your nicely-crafted contents.

    Final thoughts

    Unfortunately, this is the end of the series. Nonetheless, I think that the whole experience has been educational, since it illustrated in a friendly fashion how to build an expandable search engine by using the capabilities provided by MySQL and PHP 5.

    See you in the next PHP tutorial!



     
     
    >>> More MySQL Articles          >>> More By Alejandro Gervasio
     

       

    MYSQL ARTICLES

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek