JavaScript
  Home arrow JavaScript arrow Page 3 - Building the Server-side Component of a Se...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
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? 
JAVASCRIPT

Building the Server-side Component of a Search Engine with AJAX
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2006-11-21

    Table of Contents:
  • Building the Server-side Component of a Search Engine with AJAX
  • The search engine's client module
  • Coding the search engine's server module
  • Putting the search engine to work

  • 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


    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.

    More JavaScript Articles
    More By Alejandro Gervasio


       · Over the course of this second tutorial, this AJAX-based search engine is expanded,...
       · hello,The article was very helpful, but i have a problem how i can connect...
       · Hello Jova,Thank you for commenting on my AJAX article, and I'm glad to know it...
     

    JAVASCRIPT ARTICLES

    - More on JavaScript Array Objects
    - Methods of the DOM Location Object
    - The DOM Location Object Properties
    - Handling Remote Files with JavaScript Click ...
    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...


     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT