DESIGN AN ONLINE CHAT ROOM WITH PHP AND MYSQL

In this article, you module see how to organisation and amend a
simple online chitchat shack with PHP and MySQL. This tutorial
explains every steps of the development, including both database
design and PHP programming. Basic machine skills and knowledge
of HTML and PHP are required. Ok, let’s begin now.

Step 1: Design Database Table. Create plateau “chat” in MySQL
database to accumulation base chitchat information: chtime (chat time),
nick (user nickname) and text (chat message, inferior than 150
characters)

mysql> CREATE TABLE chitchat

-> chtime DATATIME,

-> cutting CHAR (10) NOT NULL,

-> text CHAR (150);

Step 2: Design Structure. This ultimate online chitchat shack includes
the mass quaternary sections: individual login, communication display,
message signaling and a important inclose desegregation the pass and input
sections. Thus, it needs the mass quaternary files to work:
login.php, main.php, display.php and speak.php.

Step 3: Write the code

1. login.php (just a HTML form)

Please signaling your soubriquet and enter

2. main.php

setcookie("nick",$nick) //use cake to accumulation individual soubriquet

?>

3. display.php

This enter is utilised to intend communication records from database and
display the results. To ready the filler of database, older messages
are deleted and exclusive the newest 15 messages are displayed.

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

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

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

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

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

$rows=mysql_num_rows($result);

//get the stylish 15 messages

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

//if the sort of messages<15, intend every of the messages

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

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

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

} //delete the older messages(only ready the newest 20 only)

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

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

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

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

mysql_close($link_ID);

?>

4. speak.php

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 communication achievement into database

mysql_close($link_ID); )

?>

//the mass is the communication signaling form

Now, you hit ended the organisation and writing of a ultimate online
chat system. Put every the files into your website stem and see
how it works, :)

Comments are closed.