PhpRiot
Download This Article
Download this article in PDF format with all listings and files.

Price: $5.00 AUD
(Approx. $4.10 USD)

More information
Related Books
The Essential Guide to Dreamweaver CS3 with CSS, Ajax, and PHP

The Essential Guide to Dreamweaver CS3 with CSS, Ajax, and PHP

With over 3 million users worldwide, Adobe's Dreamweaver is the most popular web development...

The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP (The Essential Guide)

The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP (The Essential Guide)

Dreamweaver CS4 is a massive step forward in terms of integration with the rest of the CS4 suite...
Browse Articles
Ajax (4), APC (1), CAPTCHA (1), CSS (3), Debugging (1), File Upload (1), Google (3), Google Maps (2), JavaScript (12), JSON (2), MVC (1), MySQL (7), onbeforeunload (1), OOP (1), PHP (28), PhpDoc (1), PostgreSQL (6), Prototype (11), Reflection (1), RFC 1867 (1), Robots (1), Scriptaculous (1), SEO (1), Sessions (1), SimpleXML (1), Smarty (5), SOAP (1), SPL (1), Templates (2), W3C (1), XHTML (1), Zend Framework (1), Zend_Search_Lucene (1)

PhpRiot Newsletter
Your Email Address:

Cloning Google Suggest With Ajaxac

Creating The Database Schema And Populating It

Now we will create our MySQL database to hold the query data. The data will be purely fictional, and obviously we won’t be able to perform actual web searches, but it will be sufficient to create some autocomplete data. We have compiled this list just by performing some sample lookups on the real Google Suggest.

  1. Create a new MySQL database. You will need to know the hostname, database name, username and password later on
  2. Populate the database with the data from data.sql

To lookup the data, this is the basic process:

  1. Retrieve the user input from the subrequest
  2. Sanitize it by removing all characters except a-z, A-Z, 0-9, as well as spaces, periods and underscores.
  3. Connect to MySQL database
  4. Perform a ‘like’ search on the _search_term_ column, retrieving up to 10 results

Notes:

  • The MySQL connection will be one of the biggest performance hits of the application, but may be difficult to get around due to the nature of PHP. Perhaps persistent connections could help some?
  • We will pre-format the returned numbers. This could potentially be done by the client to save some server processing power, but in this example we will do this in PHP.
  • The ‘like’ search should be adequate for our purposes. In the schema, the search_term column has been indexed and there will be only a single wildcard in the request.
  • I’m not sure how Google Suggest ranks its suggestion results – probably by the popularity of the search. To simulate this, we won’t order our returned results. This will also improve speed as if it had to order, it would have to find every single matching row then order them all – this way it only needs to find any 10 matching rows.

In This Article


Tagged in , , ,