Home arrow MySQL arrow Page 2 - A MySQL Driven Chat Script

A MySQL Driven Chat Script

In this article, Tim will show you how to create a quick-n-easy chat script using PHP and a very simple MySQL database. The script will allow visitors to choose a nickname, enter text to send to the chatting window, and view messages from other people... all in real-time! For a bit of fun, the chat application also has buttons to send several emote-icons including smiling and frowning faces.

Author Info:
By: Tim Pabst
Rating: 4 stars4 stars4 stars4 stars4 stars / 285
December 25, 2001
  1. · A MySQL Driven Chat Script
  2. · Creating the database
  3. · The chat script explained
  4. · The chat script explained (contd.)
  5. · The chat script explained (contd.)
  6. · Conclusion

print this article

A MySQL Driven Chat Script - Creating the database
(Page 2 of 6 )

Every time a visitor logs in to our chat room, they choose a nickname/alias. When they post a new message, it’s stored in a database along with any other messages in that chat session. At any one time, only the twenty most recent messages will be displayed in the chat window. We will create a database named “chat”. The “chat” database will contain just one table, named “chatScript”.

Start by typing “MySQL” at the command prompt to load the MySQL console app. Next, enter the commands shown below, each one separated by a new line:

create database chat;

create table chatScript


pk_Id int unsigned auto_increment,

theText varchar(100) not null,

theNick varchar(20) not null,

primary key(pk_Id),

unique id(pk_Id)


This will create our chat database and table. The “pk_Id” field is simply an auto-incrementing number, and will assign a new numerical id to each of our chat messages when they are added to the database. The “theText” field will contain the actual chat message posted by the user. The “theNick” field will contain the nickname of the user posting the message. Lastly, the “pk_Id” field is set as the primary key of the chatScript table. It is also declared as a unique id, which stops us from manually entering a duplicate value for the “pk_Id” field.

To make sure our new chat database was created successfully and is working, let’s try connecting to it through the MySQL console app and inserting a record into the chatScript table, like this:

connect chat;

insert into chatScript values(0, 'This is a test message', 'TestUser');

That’s all there is to creating the database for our chat application. Now let’s look at creating the PHP script which will allow our visitors to login and post messages.
blog comments powered by Disqus

- 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 ...

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