PHP mysql-based chat room design

Source: Internet
Author: User
Tags php mysql

Introduction: This is a detailed page of PHP mysql-based chat room design. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 323730 'rolling = 'no'>

MySQL is a database software with excellent performance, and PHP is a powerful server-side scripting language. In my website development, I 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. 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:

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. SoCodeThe following sections are usually required:

Create a page frame structure (main. php)

Show all speakersProgramSection (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.

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

<?
Setcookie ("Nick", $ Nick) // use 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:

<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

<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
<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. Of course, designers can make some personalized designs based on their personal interests, such as adding a page box to display the list of people in the current chat room, adding comments, obtaining the IP address of the speaker, and further beautifying the page.

More articles on "php mysql-based chat room design"

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/323730.html pageno: 16.

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.