SunQuest
 
       MySQL
  Home arrow MySQL arrow Page 4 - Completing a Search Engine with MySQL ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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? 
MYSQL

Completing a Search Engine with MySQL and PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Over the course of this final tutorial of the series, this MySQL/PHP 5 - based...
       · It's a decent simple search engine, but that's all really. It gets tougher when you...
       · Thank you for commenting on my PHP article. You’re correct regarding the simplicity...
       · Getting a grasp on classes in php is difficult for some, like myself, but your...
       · Thank you for the kind comments on my PHP article, and it's always good to know it's...
       · Hi, Thanks for the detailed tutorial. I began with confidence but I have...
       · Thank you for the comments on my PHP article. Now, concerning your question, the...
       · Hi Alejandro,Thank you for taking your time to reply. I read your entire...
       · Hi JSP,Thanks again for posting here. Now that you've created a "processform.php"...
       · Thanks Alejandro,That helps... WOW I wouldn't have guessed that, since I am no...
       · Hi JSP,Glad to know the search engine is working for you. Thanks again for the...
     

       

    MYSQL ARTICLES

    - 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...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...
    - Creating the Blog Script for a PHP/MySQL Blo...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway