Home arrow MySQL arrow Page 4 - Completing a Search Engine with MySQL and PHP 5

Developing a fully-functional practical example - MySQL

Building database-driven web sites is one of the most popular trends today in web site development. However, this approach implies that potential visitors must be provided with a straightforward mechanism that allows them to search through web site content. This three-part series walks you through the process of building an expandable search engine by using the combined functionality of MySQL and PHP 5.

TABLE OF CONTENTS:
  1. Completing a Search Engine with MySQL and PHP 5
  2. Listing the full source code of the original search application
  3. Defining a simple web page generating class
  4. Developing a fully-functional practical example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 12
August 13, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

MYSQL ARTICLES

- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: