XML
  Home arrow XML arrow Page 3 - Building an AJAX-Based Chat: Interacting W...
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  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
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? 
XML

Building an AJAX-Based Chat: Interacting With a Database
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 32
    2005-11-21

    Table of Contents:
  • Building an AJAX-Based Chat: Interacting With a Database
  • Adding messages to the database: looking at the “sendchatdata.php” file
  • Reading messages from the server: defining the “getchatdata.php” file
  • Putting the files together: the complete chat application at a glance

  • 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 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',
    'password'=>'password','database'=>'chat'));
    // retrieve last 20 messages
    $db->query("SELECT user,message FROM messages
    ORDER BY id DESC LIMIT 20");
    // send messages to the client
    while($row=$db->fetchRow()){
        echo '<'.$row['user'].'>'.$row
    ['message'].'|';
    }

    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 (
        id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT
    PRIMARY KEY,
        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.

    More XML Articles
    More By Alejandro Gervasio


       · This last article of the series goes through the development of the PHP scripts...
       · Great article, thanks!
       · Thank you for the compliments on my AJAX article.Regards.
       · Hi Alejandro,again a really interesting article. Thanks a lot!One question...
       · Hello again,Thank you for the positive comments on my article. Comming to your...
       · Thanks for your quick reply Alejandro. And good to get your confirmation that it...
       · Hello Matthijs,I'd like to thank you for reading the tutorial. Good...
       · Hi Alejandro! Thank you very much for your great AJAX chat tutorial. It has helped...
       · I solved it! I just added to the sendMessage function:...
       · Fantastic article you've really helped me to understand.But I have a question...
       · As posted earlier by someone else, when pressing return on a message, it does not...
       · Thanks a lot for your comments on the article. Also, I'm glad to know that you found...
       · Hello,Thanks for reading my article. Regarding your question, that happens...
       · Thank you for your comments. With reference to your question, you should modify the...
       · Create a new function:function submitSend(){ ...
       · Thank you for submitting your feedback on this article. Yeap, creating a function...
       · the article by Alejandro Gervasio is really brilliant, it worked first time, and it...
       · Thank you for your compliments on my AJAX article, since they're very welcome. I'm...
       · Your tip worked for me in Firefox. But in IE I still couldn't use the enter key. I...
       · Thank you for your feedback. Also, it's good to hear that my suggestion worked for...
       · Hello!First of all, congratulations for the great article. It really helped me...
       · Hello Lucas,Thank you for your kind comments on my article. I've tested the chat...
       · Hi guys!I created a control that works like an AJAX container. Basically the way it...
       · Thanks a lot for posting your comments here. Even when I'm not well versed on...
       · Thank you very much for your explanation. I wouldn't say that I understand...
       · Hi again,Sorry to hear that the http headers didn't solve your problem. However...
       · hey sorry for the inconvencience...but after a while I got the chatfunction to...
       · Hello,Good to know that the http headers now worked for you. Concerning your...
       · Hello, Sorry it's me again.I'm working on a online/offline function. I have one...
       · Hello again,It's good to see the way that you redefined the structure of the...
       · Well that's the problem.. if I for instance would use this code:function...
       · Hello again,I see you're still in problems, but there's one thing that came up...
       · Well thanks for you help.. And your idea is a good one.. But i figured out what...
       · Hi again,I'm glad to know you figured out how to solve the problem. Good luck...
       · I have been trying to implement smilies, but whenever i convert certain char strings...
       · Thanks for the comment on my AJAX article. Concerning your question, you should...
       · Thanks! it worked great.However after implementing this code the name no longer...
       · I'm glad to know the suggestions I made previously worked for you concerning the...
       · strangely enough the...
       · Hello,Please check the above post, in order to get an idea on how to include...
       · Thank you for your well organized and refreshing explanation of a simple Ajax...
       · Hello Bob,First off, I’d like to thank you for your kind comments on my AJAX...
       · Found the article fantastic. Very insightful. Was wondering if one could easily...
       · Thanks for the kind words on my Ajax article. Regarding your question, a current...
     

    XML ARTICLES

    - 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
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE
    - UI Design with Java and XML Toolkits
    - Displaying ADO Retrieved Data with XML Islan...
    - Widget Walkthrough
    - Introduction to Widgets
    - The Why and How of XML Data Islands







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway