Use Php+mysql to build a chat room

Source: Internet
Author: User
Tags date current time html page mysql in
MySQL concurrency is strong, fast response, excellent performance of the database software; PHP is a powerful server-side scripting language. The author in Shanxi aluminum Plant website development, uses php4.0+mysql3.23.38 to establish many kinds of applications. Below, take a simple chat room design as an example, introduce the application of Php+mysql in Web page development.

1. Overall design

1.1 Conception and Planning:

The basic principle of the chat room is to store the speech data of each user who is connected to the same Web page, and then pass all the speech data to each user. That is, using a database to assemble everyone's speeches, and pass the data in the database to everyone, realizes the function of the chat room.

1.2 Table Design

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

mysql> CREATE TABLE Chat
-> (Chtime datatime,
-> Nick CHAR (Ten) not NULL,
->words CHAR (150));
The table only sets three domains, Chtime is the time to speak, Nick is the speaker's nickname, words is the content of the speech, speak up to 150 characters

1.3 Web Design

One of the simplest chat rooms usually requires two page boxes: a page box is a form where the user enters the statement, and the other is used to show the speaker. So code snippets usually require at least the following paragraphs:

Build the structure of the page frame (main.php)

section of the program showing members ' statements (cdisplay.php)

Segment of the program transmitting user statements (speak.php)

User Login into Chat room program section (login.php)

2. Code Design

After the completion of the above plan, you can start code design, PHP can be very concise to achieve the above functions.

2.1 User Login login.php, this code is a full HTML page

<title> User logon </title>
<body> Please 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>
After the user submits his nickname, he enters into the chat room, and the following processing is referred to main.php for processing.

2.2 Page Frame body code segment main.php:

?
Setcookie ("Nick", $nick)//using cookies to record user nicknames is a common method of passing variables
? >

<title> Shanxi Aluminum Factory chat room trial version ver1.0 </title>
<frameset rows= "80%,*"
<frame src= "cdisplay.php" name= "Chatdisplay"
<frame src= "speak.php" name= "speak"
</frameset>
2.3 Show Speeches cdisplay.php

The task of this code snippet is to remove the data from the table chat and display it in the page box. Each time you refresh, take the last 15 statements in the database. At the same time, to prevent the database from increasing indefinitely, it is necessary to design the function of deleting stale data. The code is as follows

<title> Show users to speak </title>
<meta http-equiv= "Refresh" content= "5;url=cdisplay.php"
<body>
?
$link _id=mysql_connect ("main", "root");
Link MySQL server server name is main, administrator name is root
mysql_select_db ("abc"); Select 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 record pens to get query results
Get the last 15 statements and show
@mysql_data_seek ($resut, $rows-15); Move record pointer to top 15 records
if ($rows <15) $l = $rows; else $l = 15; The total number of records is less than 15, then the maximum number of records
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 obsolete data in a library
@mysql_data_seek ($result, $rows-20); Move record pointer to top 20 records
List ($limtime) =mysql_fetch_row ($result);
$str = "DELETE from chat WHERE chtime< ' $limtime ';";
$result =mysql_query ($str, $link _id); Send out query string with only Top 20 records in library
Mysql_close ($link _id);
? >
</body>
2.4 Send out speak to database speak.php

<title> speak </title>
<body>
?
If ($words)
{$link _id=mysql_connect ("main", "root");
mysql_select_db ("abc"); Database name is ABC
$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); Send out statements to the database
Mysql_close ($link _id);
}
? >

Enter a form to speak

<form action= "speak.php" method= "post" target= "_self"
<input type= "text" name= "words" cols= "20"
<input type= "Submit" value= "speech"
</form>
</body>
After the completion of the above work, a simple chat room production is completed. Of course, designers can do some personalized design according to their own hobbies, such as adding a page box, displaying the current chat room list, increasing the expression, getting the speaker IP, further beautifying the page, and so on.

Related 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.