Home arrow XML arrow Page 3 - Building an AJAX-Based Chat: Interacting With a Database

Building an AJAX-Based Chat: Interacting With a Database

In this third and final part of a tutorial that explains how to build an AJAX-based chat application, Alejandro Gervasio covers creating the PHP files that directly interact with a MySQL database, adding and fetching chat messages. He also makes some suggestions for expanding the functions of the application.

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 52
November 21, 2005
  1. · Building an AJAX-Based Chat: Interacting With a Database
  2. · Adding messages to the database: looking at the “sendchatdata.php” file
  3. · Reading messages from the server: defining the “getchatdata.php” file
  4. · Putting the files together: the complete chat application at a glance

print this article

Building an AJAX-Based Chat: Interacting With a Database - Reading messages from the server: defining the “getchatdata.php” file
(Page 3 of 4 )

As I mentioned earlier, the “getchatdata.php” file performs a smaller task. It only fetches the last twenty messages from the database in descending order and sends the data back to the client to be processed. By sticking to the same approach I tackled for adding messages to the database, I’ll use the MySQL abstraction class for running SELECT queries. The definition for this file is the following:

// include class file
require_once 'mysql_class.php';
// connect to MySQL
$db=&new MySQL(array('host'=>'host','user'=>'user',
// retrieve last 20 messages
$db->query("SELECT user,message FROM messages
// send messages to the client
    echo '<'.$row['user'].'>'.$row

If you’re patient enough to compare the above file with the one I wrote for inserting chat messages, you’ll realize that they’re closely similar. Undoubtedly, I could have merged both files into one, but I decided to keep them as separate pieces of code, so they have clearly delimited tasks and the overall application is easier to read and understand.

In this case, the above file connects to MySQL, then runs the SELECT query and fetches the first twenty rows. Finally, they’re formatted and transmitted back to the client, for processing by the JavaScript functions that I wrote in my previous articles.

At this point, the files parsed on the server have been properly defined, which demonstrates how messages are pushed into and pulled out from the database. Now, the reason for working with two mutually independent requester objects should be clear. Given that, let’s complete the server-side code by defining the SQL statements that create a sample “messages” table in MySQL. It is as follows:

CREATE TABLE messages (
    user CHAR(256) NOT NULL,
    message CHAR(256) NOT NULL

That’s it. The above statements build up the “messages” table, useful for storing user nicknames and chat messages. Now, having defined the two complementary PHP files that access the chat database, as well as the simple SQL code for creating the pertinent database table, the next step involved in developing the AJAX-based chat will be listing all the files that integrate the whole application. So, are you ready to digest lengthy code? Fine, keep on reading.

blog comments powered by Disqus

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials