Home arrow MySQL arrow Page 3 - Building a Search Engine with MySQL and PHP 5

Performing searches with MySQL and PHP 5 - MySQL

If you maintain a medium-sized, growing web site, you might find that it needs an internal proprietary search engine to improve your visitors' experience. This article, the first of three parts, will get you started with building such an engine using PHP and MySQL.

  1. Building a Search Engine with MySQL and PHP 5
  2. Creating the front end
  3. Performing searches with MySQL and PHP 5
  4. Assembling the modules of the MySQL-driven search application
By: Alejandro Gervasio
Rating: starstarstarstarstar / 54
July 31, 2007

print this article



As you might have guessed, developing the business logic that drives this MySQL-based search application is reduced to defining a couple of PHP classes. These classes will be responsible for performing some crucial tasks, such as connecting to MySQL and executing a specific SELECT statement, as well as returning to the browser the corresponding database results in accordance with the search terms entered on the online form that you saw previously.

To perform all of the aforementioned tasks, below I defined two basic PHP classes. You'll probably find them quite familiar, since I have used them with some of my previous articles on PHP development published here on the prestigious Developer Shed network.

The classes in question have been wrapped into one single file, called "mysql.php", whose signature looks like this:

(definition of "mysql.php" file)


// define 'MySQL' class

class MySQL{

private $conId;

private $host;

private $user;

private $password;

private $database;

private $result;

const OPTIONS=4;

public function __construct($options=array()){


throw new Exception('Invalid number of connection parameters');


foreach($options as $parameter=>$value){


throw new Exception('Invalid parameter '.$parameter);






// connect to MySQL

private function connectDB(){


throw new Exception('Error connecting to the server');



throw new Exception('Error selecting database');



// run query

public function query($query){


throw new Exception('Error performing query '.$query);


return new Result($this,$this->result);


public function escapeString($value){

return mysql_escape_string($value);



// define 'Result' class

class Result {

private $mysql;

private $result;

public function __construct(&$mysql,$result){




// fetch row

public function fetchRow(){

return mysql_fetch_assoc($this->result);


// count rows

public function countRows(){


return false;


return $rows;


// count affected rows

public function countAffectedRows(){


throw new Exception('Error counting affected rows');


return $rows;


// get ID form last-inserted row

public function getInsertID(){


throw new Exception('Error getting ID');


return $id;


// seek row

public function seekRow($row=0){


throw new Exception('Invalid result set offset');



throw new Exception('Error seeking data');





As illustrated above, the previous PHP file uses a pair of MySQL processing classes to perform a search process against one or more selected databases. Also, you should notice that the versatile and extensible structure offered by the above PHP classes makes it really easy to implement an internal search engine on any existing web site, a feature that speaks for itself about the expandability of this MySQL-based search application.

So far, so good. At this time you hopefully grasped the business logic that stands behind this search engine, since it only requires a few simple PHP classes to perform the corresponding searches on the MySQL server. So what's the next step? 

Well, assuming that you may want to see how all the previously defined files can be linked with each other to implement a fully-functional search application, in the following section I'm going to develop an illustrative example to show you how to put this MySQL-based search engine to work quickly.

To learn how this hands-on example will be developed, please click on the link that appears below and keep reading.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: