PHPMySQL-based chat room design

Source: Internet
Author: User
MySQL features high concurrency and fast response speed. it is a database software with excellent performance. PHP is a powerful server-side scripting language. In my website development, I used PHP4.0 + MySQL3.23.38 to establish multiple exploitation methods. Below, a simple chat room design

MySQL features high concurrency and fast response speed. it is a database software with excellent performance. PHP is a powerful server-side scripting language. In my website development, I used PHP4.0 MySQL3.23.38 to establish multiple exploitation methods. Next, we will take a simple chat room design as an example to introduce the use of PHP MySQL in web development.

1. Overall design

1. 1 Conception and plan:

The basic principle of the 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 function of chatting rooms.

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 for the user to enter 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)

User login into the chat room program section (login. php)

2. code design

After the above plan is completed, you can start code design. using php can be very concise to achieve the above functions.

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

  
  
  User logon
  
  Enter your nickname

  


  
  
  
  

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

Main. php:

Setcookie ('Nick ',) // uses cookies to record user nicknames. it is a common method for passing variables.
?>
  
  Trial version of Shanxi Aluminum plant chat room ver1.0
  
  
  
  
  

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 need to design the function of deleting stale data. The code is as follows:

  
  
  Show user speeches

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.