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.
Next: The chat script explained >>
More MySQL Articles
More By Tim Pabst