MySQL
  Home arrow MySQL arrow Page 5 - A MySQL Driven Chat Script
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? 
MYSQL

A MySQL Driven Chat Script
By: Tim Pabst
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 200
    2001-12-25

    Table of Contents:
  • A MySQL Driven Chat Script
  • Creating the database
  • The chat script explained
  • The chat script explained (contd.)
  • The chat script explained (contd.)
  • Conclusion

  • 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


    A MySQL Driven Chat Script - The chat script explained (contd.)


    (Page 5 of 6 )

    The ShowAddPosts function

    This is the most important function of the entire chat script, and is responsible for adding new messages to the database as well as display the last twenty messages posted. Firstly, the ShowAddPosts function declares the $chat and $nick variables as global. This will give us access to the message being posted (if any), as well as the users nickname, which is stored as a session variable:

    global $HTTP_SESSION_VARS;

    global $chat;

    global $nick;


    To make sure our visitor sees all of the newest messages, we add a <meta> tag to the HTML page, which will cause it to refresh itself every ten seconds:

    print '<meta http-equiv="refresh" content="10;URL=chat.php?action=posts&nick=<?php echo $nick; ?>">';

    Then we create a connection to the “chat” database and select the “chatScript” table. The results of these functions are stored in the $svrConn and $dbConn variables respectively:

    $svrConn = mysql_connect("localhost", "admin", "password") or die("<b>Error:</b> Couldnt connect to database");

    $dbConn = mysql_select_db("chat", $svrConn) or die ("<b>Error:</b> Couldnt connect to database");


    You should change the value of “admin” to a valid username for your database. Also, change the value of “password” to a valid password for your database.

    Next, the script checks to see whether or not the user is posting a message. If he/she is, then the $chat variable will hold the message. The $chat variable is automatically created by PHP from the <input type=”text” name=”chat”> form element, which is where we enter the message. An SQL query is executed, which inserts the new message into the database:

    if(!empty($chat)) {

    $strQuery = "insert into chatScript values(0, '$chat', '$nick')";

    mysql_query($strQuery);

    }


    Once the new message is inserted into the database, we run a SELECT query to get the twenty most recent posts, which will be ordered based on the “pk_Id’ field in descending order:

    $strQuery = "select theText, theNick from chatScript order by pk_Id desc limit 20";

    $chats = mysql_query($strQuery);


    The $chats variable will contain the result of the SQL query. We simply loop through those results, displaying each record one at a time:

    while($chatline = mysql_fetch_array($chats)) {

    print "<b>" . $chatline["theNick"] . ":</b> " . swapFaces($chatline["theText"]) . "<br>";

    }


    The actual message is passed to the “swapFaces” function, which replaces “:)”, “:(“ and “:D” with an <img> tag to display the corresponding image for that icon-icon. The swapFaces function looks like this:

    function swapFaces($chatLine) {

    $chatLine = str_replace(":)", "<img src='smile.gif'>", $chatLine);

    $chatLine = str_replace(":(", "<img src='frown.gif'>", $chatLine);

    $chatLine = str_replace(":D", "<img src='bigsmile.gif'>", $chatLine);

    return $chatLine;

    }


    One last thing to mention is that each time a form is submitted, a hidden field named “action” is also passed along with it. This tells the PHP script which function to call. We use an if…else control to redirect our script to the appropriate function, like this:

    if (empty($action))

    ShowLoginForm();

    elseif ($action == "posts")

    ShowAddPosts();

    elseif ($action == "form")

    GetInput();

    elseif ($action == "enter")

    Login();


    That’s the code for our simple chat script explained. Take a look at the screen shot below (it shows me and two of my friends conversing over nothing):

    Our simple chat script in action!

    More MySQL Articles
    More By Tim Pabst


       · not work
       · The code does not work at all in its present form.A number of bugs are there. For...
       · this is i have coppied and tested . but it is not working right. pls check and reply...
       · The code is absolutely rubbish. It does not work. A dirty joke by the writter
       · We're sorry the code doesn't work for you. I think you will find that our more...
     

    MYSQL ARTICLES

    - MySQL and BLOBs
    - Two Lessons in ASP and MySQL
    - Lord Of The Strings Part 2
    - Lord Of The Strings Part 1
    - Importing Data into MySQL with Navicat
    - Building a Sustainable Web Site
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - PhpED 3.2 – More Features Than You Can Poke ...
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - Security and Sessions in PHP
    - Setup Your Personal Reminder System Using PHP
    - Create a IP-Country Database Using PERL and ...
    - Developing a Dynamic Document Search in PHP ...






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