Use PHP + MySQL to set up the chat room function instance code
Source: Internet
Author: User
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:
User logon
Enter your nickname
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.
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.