Chat Room Design with PHP and MySQL

 


Visitors: 677

In this article, you will learn how to design and develop a simple online chat room with PHP and MySQL. This tutorial explains every steps of the development, including both database design and PHP programming. Basic computer skills and knowledge of HTML and PHP are required. Ok, let's begin now.

Step 1: Design Database Table. Create table “chat" in MySQL database to store basic chat information: chtime (chat time), nick (user nickname) and words (chat message, less than 150 characters)

mysql> CREATE TABLE chat

-> chtime DATATIME,

-> nick CHAR (10) NOT NULL,

-> words CHAR (150);

Step 2: Design Structure. This simple online chat room includes the following four sections: user login, message display, message input and a main frame integrating the display and input sections. Thus, it needs the following four files to work:

login.php

main.php

display.php

speak.php

Step 3: Write the code

1. login.php (just a HTML form)

User Login

Please input your nickname and enter

<br>

<form action="main.php" method="post" target="_self">

<input type="text" name="nick" cols="20">

<input type="submit" value="login">

</form>

2. main.php

setcookie("nick", $nick) //use cookie to store user nickname

?>

My Chat Room

<frameset rows="80%, *">

<frame src="display.php" name="chatdisplay">

<frame src="speak.php" name="speak">

</frameset>

3. display.php

This file is used to get message records from database and display the results. To keep the size of database, old messages are deleted and only the newest 15 messages are displayed.

Display Messages

//connect to mysql server, server name: main, database username: root

$link_ID=mysql_connect("main", "root");

mysql_select_db("abc"); //abc is the database name

$str="select * from chat ORDER BY chtime;" ;

$result=mysql_query($str, $link_ID);

$rows=mysql_num_rows($result);

//get the latest 15 messages

@mysql_data_seek($resut, $rows-15);

//if the number of messages<15, get all of the messages

if ($rows<15) $l=$rows; else $l=15; for ($i=1;$i<=$l; $i++) {

list($chtime, $nick, $words)=mysql_fetch_row($result);

echo $chtime; echo " “; echo $nick; echo":" ; echo $words;

echo “

<br>";

} //delete the old messages(only keep the newest 20 only)

@mysql_data_seek($result, $rows-20);

list($limtime)=mysql_fetch_row($result);

$str="DELETE FROM chat WHERE chtime<'$limtime’ ;" ;

$result=mysql_query($str, $link_ID);

mysql_close($link_ID);

?>

4. speak.php

Speak

If ($words)

{ $link_ID=mysql_connect("main", "root");

mysql_select_db("abc"); // abc is the database name

$time=date(y). date(m). date(d). date(h). date(i). (date(s); //get current time

$str="INSERT INTO chat(chtime, nick, words) values ('$time', '$nick', '$words');" ;

mysql_query($str, $link_ID); //save message record into database

mysql_close($link_ID); )

?>

//the following is the message input form

<form action="speak.php" method="post" target=" _self">

<input type="text" name="words" cols="20">

<input type="submit" value="Speak">

</form>

Now, you have finished the design and coding of a simple online chat system. Put all the files into your website root and see how it works, :)

Rory Canyons is the founder of ScriptMenu.com . Fore more articles, scripts or tips for PHP, ASP, ASP. NET, PERL, XML, Java, JavaScript, Flash, CFML, Python and other web programming resources, visit http://www.scriptmenu.com

(799)
Tags:
chat chatroom php mysql

Article Source:


 
Rate this Article: 
 
The Chat Room Addict, Teen Chat Rooms and the Possible Dangers
Rated 4 / 5
based on 5 votes
ArticleSlash

Related Articles:

The Use of PHP and MySQL

by: Helen Cox (July 03, 2008) 
(Internet and Businesses Online/Web Development)

Introduction to MySQL with PHP

by: Haysam Eltabl (October 24, 2005) 
(Internet and Businesses Online/Web Development)

MySQL Database Handling in PHP

by: John L (March 19, 2005) 
(Internet and Businesses Online/Web Development)

Apache, MySQL & PHP for Windows

by: Sanjib Ahmad (June 12, 2005) 
(Computers and Technology/Personal Tech)

Newsletter Management Using PHP w/o mySQL for Beginners

by: Jeremy Miller (August 14, 2005) 
(Internet and Businesses Online/Web Development)

Developing A Login System With PHP And MySQL

by: John L (January 19, 2005) 
(Internet and Businesses Online/Web Development)

For Automated Sites - PHP and MySQL Are A Perfect Match

by: Halstatt Pires (September 15, 2005) 
(Internet and Businesses Online/Web Design)

Creating Dynamic Website Content with PHP - MySQL

by: Don Beavers (June 08, 2005) 
(Internet and Businesses Online/Web Development)

Using PHP and MySQL to Develop a Simple CMS - Version 2

by: John Dixon (February 22, 2008) 
(Internet and Businesses Online/Web Development)

The Chat Room Addict, Teen Chat Rooms and the Possible Dangers

by: Jerome Carter (May 28, 2007) 
(Self Improvement)