Tutorial on setting up a chat room in phpmysql

Source: Internet
Author: User
Many chat room programs can be implemented, and the principle is simple as long as there is no need to update it. the simplest thing is to use an iframe to constantly refresh the page and read user records from the database. MySQL is a database with excellent performance because of its strong concurrent capabilities and fast response speed...

Many chat room programs can be implemented, and the principle is simple as long as there is no need to update it. the simplest thing is to use an iframe to constantly refresh the page and read user records from the database.

MySQL is a database software with excellent performance, and PHP is a powerful server-side scripting language. In the development of Shanxi Aluminum Factory website, I have used PHP4.0 + MySQL3.23.38 to establish a variety of applications. Next, we will take a simple chat room design as an example to introduce the application of PHP + MySQL in web development.

1. Overall design

1.1 Conception and planning:

The basic principle of a chat room is to store the speech data transmitted by each user connected to the same webpage, and then transmit all the speech data to each user. That is to say, using a database to gather everyone's speeches and passing the data in the database to everyone realizes the chat room function.

1.2 Table design

First, use MySQL to create a table chat to store users' speeches:

The code is as follows:

Mysql> create table chat
-> (Chtime DATATIME,
-> Nick CHAR (10) not null,
-> Words CHAR (150 ));


The table only sets three Fields. the chtime is the time when the speaker speaks, nick is the nickname of the speaker, and words are the content of the speech. the speech can contain a maximum of 150 characters.

1.3 webpage design

A simple chat room usually requires two page boxes: one is the form in which the user inputs the speech, and the other is used to display the speech. Therefore, the code segment usually requires at least the following sections:

Create a page frame structure (main. php)

Show the program section (cdisplay. php)

Program section for sending user speeches (speak. php)

Log on to the chat room program section (login. php)

2. code design

After the above planning is complete, you can start code design. using php, you can easily implement the above functions.

2.1 log on to login. php. the code in this section is a complete HTML webpage.

The code is as follows:
<Html>
<Head>
<Title> user logon </title>
</Head>
<Body> enter your nickname <br>
<Form action = "main. php" method = "post" target = "_ self">
<Input type = "text" name = "nick" cols = "20">
<Input type = "submit" value = "login">
</Body>
</Html>


After the user submits his nickname, he enters the chat room. the following processing is handled by main. php.

Main. php:

The code is as follows:

<?
Setcookie ("nick", $ nick) // uses cookies to record user nicknames. it is a common method for passing variables.
?>

<Html>
<Title> trial version of Shanxi Aluminum Factory chat room ver1.0 </title>
<Frameset rows = "80%, *">
<Frame src = "cdisplay. php" name = "chatdisplay">
<Frame src = "speak. php" name = "speak">
</Frameset>
</Html>


2.3 presentation cdisplay. php

The task of this code snippet is to retrieve the data in the chat table and display it in the page box. Each time you refresh, take the last 15 statements in the database. In addition, to prevent the database from increasing infinitely, you must design the function of deleting old data. The code is as follows:

The code is as follows:
<Html>
<Head>
<Title> show user speeches </title>
<Meta http-equiv = "refresh" content = "5; url = cdisplay. php">
</Head>
<Body>
<?
$ Link_ID = mysql_connect ("main", "root ");
// Link the Mysql server name to main and the administrator name to root
Mysql_select_db ("abc"); // select a database
$ Str = "select * from chat order by chtime;"; // query string
$ Result = mysql_query ($ str, $ link_ID); // send a query
$ Rows = mysql_num_rows ($ result); // number of records that obtain the query result
// Get the last 15 speeches and display them
@ Mysql_data_seek ($ resut, $ rows-15); // move the record pointer to the first 15 records
If ($ rows <15) $ l = $ rows; else $ l = 15; // The maximum number of records is 15 if the total number of records is less than 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> ";
}
// Clear outdated data in the database
@ Mysql_data_seek ($ result, $ rows-20); // move the record pointer to the first 20 records
List ($ limtime) = mysql_fetch_row ($ result );
$ Str = "delete from chat WHERE chtime <'$ limtime ';";
$ Result = mysql_query ($ str, $ link_ID); // send the query string, leaving only the first 20 records in the database
Mysql_close ($ link_ID );
?>
</Body>
</Html>

2.4 send a speech to the database speak. php

The code is as follows:
<Html>
<Head>
<Title> speech </title>
</Head>
<Body>
<?
If ($ words)
{$ Link_ID = mysql_connect ("main", "root ");
Mysql_select_db ("abc"); // The database name is abc.
$ Time = date (y). date (m). date (d). date (h). date (I). (date (s); // get the current time
$ Str = "insert into chat (chtime, nick, words) values
('$ Time',' $ nick ',' $ words ');";
Mysql_query ($ str, $ link_ID); // sends a speech to the database
Mysql_close ($ link_ID );
}
?>

// Enter the speech form

The code is as follows:

<Form action = "speak. php" method = "post" target = "_ self">
<Input type = "text" name = "words" cols = "20">
<Input type = "submit" value = "">
</Form>
</Body>
</Html>


After completing the above work, a simple chat room is created.



Article address:

Reprint at will ^ please include the address of this article!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.