Building the Server-side Component of a Search Engine with AJAX - Coding the search engine's server module
(Page 3 of 4 )
Since all the processes for performing the search against a selected database will be implemented with PHP 5, then logically I'll first create a couple of straightforward MySQL processing classes. These classes will take care of doing all sorts of clever tasks, like connecting to the server, selecting a particular database, handling MySQL result sets, and so forth.
That being said, here are the respective definitions for these two new PHP 5 classes:
// 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()){
if(count($options)!=self::OPTIONS){
throw new Exception('Invalid number of
connection parameters');
}
foreach($options as $parameter=>$value){
if(!$value){
throw new Exception('Invalid
parameter '.$parameter);
}
$this->{$parameter}=$value;
}
$this->connectDB();
}
// connect to MySQL
private function connectDB(){
if(!$this->conId=mysql_connect($this->host,
$this->user,$this->password)){
throw new Exception('Error connecting to the
server');
}
if(!mysql_select_db($this->database,$this->conId)){
throw new Exception('Error selecting
database');
}
}
// run query
public function query($query){
if(!$this->result=mysql_query($query,$this->conId)){
throw new Exception('Error performing
query '.$query);
}
return new Result($this,$this->result);
}
}
// define 'Result' class
class Result {
private $mysql;
private $result;
public function __construct(&$mysql,$result){
$this->mysql=&$mysql;
$this->result=$result;
}
// fetch row
public function fetchRow(){
return mysql_fetch_assoc($this->result);
}
// count rows
public function countRows(){
if(!$rows=mysql_num_rows($this->result)){
return '0';
}
return $rows;
}
// count affected rows
public function countAffectedRows(){
if(!$rows=mysql_affected_rows($this->mysql->conId)){
throw new Exception('Error counting affected
rows');
}
return $rows;
}
// get ID from last-inserted row
public function getInsertID(){
if(!$id=mysql_insert_id($this->mysql->conId)){
throw new Exception('Error getting ID');
}
return $id;
}
// seek row
public function seekRow($row=0){
if(!is_int($row)||$row<0){
throw new Exception('Invalid result set
offset');
}
if(!mysql_data_seek($this->result,$row)){
throw new Exception('Error seeking data');
}
}
}
At this point, after you grasped the logic followed by the couple of PHP classes shown above, it's clear to see here that the only methods that I'm going to use are those that are useful for connecting to MySQL, as well as for running queries and fetching rows from a selected database table. It makes sense, doesn't it?
Having listed the previous PHP 5 classes responsible for performing searches in the server, let me explain briefly how I plan to use them to implement a functional search engine. Basically, since the application will be used as part of a fictional dynamic website, I'll assume that there's a unique database table called "pages," containing the fields "ID," "Title" and "Contents" respectively.
Logically, the "ID" field will store the identifier of each dynamic web page that comprises the whole site. The "Title" field will house the web document's title, and finally the "Contents" column will contain the dynamic contents delivered to visitors. Of course, this is only a basic database schema that can be easily adapted to work with larger websites, but for now, it's more than enough for demonstrating how this search engine will work.
Speaking of that, in the next few lines I'll show you how to put the AJAX-based search application to work by including some basic PHP code. Click on the link below and keep reading.
Next: Putting the search engine to work >>
More JavaScript Articles
More By Alejandro Gervasio