Chat room design based on PHP+MySQL_PHP tutorial
In website development, the author used PHP4.0+MySQL3.23.38 to build a variety of applications. Next, take a simple chat room design as an example to introduce the application of PHP+MySQL in web development.
1. Overall design
1. 1 Concept and planning:
The basic principle of the chat room is to store the speech data sent by each user connected to the same web page, and then transmit all the speech data to each user. In other words, the function of a chat room is realized by using a database to collect everyone's speeches and transmitting the data in the database to everyone.
1.2 Table design
First use MySQL to create a table chat to store user comments:
mysql> CREATE TABLE chat
-> (chtime DATATIME,
-> nick CHAR(10) NOT NULL,
->words CHAR(150));
Only three fields are set in the table, chtime is the time of speaking, nick is the nickname of the speaker, words is the content of the speech, and the speech can be up to 150 characters
1.3 Web Design
The simplest chat room usually requires two page frames: one page frame is the form for users to enter comments, and the other is used to display everyone's comments. So the code segment usually requires at least the following segments:
Create the structure of the page frame (main.php)
Display the program segment where everyone speaks (cdisplay.php)
Transmit the program segment (speak.php) where the user speaks
User login to enter the chat room program segment (login.php)
2. Code design
After the above planning is completed, you can start code design. Using PHP can realize the above functions very simply.
2.1 User login login.php, this code is a complete HTML web page
Please enter your nickname
After the user submits his nickname, he enters the chat room, and the following processing is handled by main.php.
2.2 Main code segment of page frame main.php:
setcookie("nick",$nick) //Use Cookie records user nickname, which is a commonly used method of passing variables
?>
< frameset rows="80%,*">
2.3 Display speech cdisplay.php
The task of this code segment is to extract the data from the chat table and display it in the page frame. Each time it is refreshed, the 15 most recent statements in the database are fetched. At the same time, in order to prevent the database from growing indefinitely, it is necessary to design the function of deleting old data. The code is as follows
$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 query
$rows=mysql_num_rows($result); //Get the number of records of query results
//Get the last 15 statements , and display
@mysql_data_seek($resut,$rows-15); //Move the record pointer to the first 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 is
for ($i=1;$i<=$l;$i++) {
list($chtime,$nick,$words)=mysql_fetch_row ($result);
echo $chtime; echo " ";echo $nick; echo":" ; echo $words; echo "
";
}
//Clear obsolescence in the library Data
@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 library
mysql_close($link_ID) ;
?>
2.4 Send speech to database speak.php
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); //Send a statement to the database
mysql_close($link_ID);
}
?>
//Enter the form for making a statement
After completing the above work , a simple chat room is completed. Of course, the designer can make some personalized designs according to personal preferences, such as adding a page frame to display the list of current chat room members, adding speaking emoticons, obtaining the speaker's IP, and further beautifying the page. Wait.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



According to news on March 4, Kubi Rubik's Cube will launch the "Xiaoku Tablet 2Lite" tablet computer on March 5, with an initial price of 649 yuan. It is reported that the new tablet is equipped with Unisoc’s T606 processor, which uses a 12nm process and consists of two 1.6GHz ArmCortex-A75 CPUs and six ArmCortex-A55 processors. The screen uses a 10.95-inch IPS eye-protection screen with a resolution of 1280x800 and a brightness as high as 350 nits. In terms of imaging, Xiaoku Tablet 2Lite has a 13-megapixel main camera on the rear and a 5-megapixel selfie lens on the front. It also supports 4G Internet access/calls, Bluetooth 5.0, and Wi-Fi5. In addition, the official claimed that this tablet&l

According to news on April 17, HMD teamed up with the well-known beer brand Heineken and the creative company Bodega to launch a unique flip phone - The Boring Phone. This phone is not only full of innovation in design, but also returns to nature in terms of functionality, aiming to lead people back to real interpersonal interactions and enjoy the pure time of drinking with friends. Boring mobile phone adopts a unique transparent flip design, showing a simple yet elegant aesthetic. It is equipped with a 2.8-inch QVGA display inside and a 1.77-inch display outside, providing users with a basic visual interaction experience. In terms of photography, although it is only equipped with a 30-megapixel camera, it is enough to handle simple daily tasks.

According to news on April 26, ZTE’s 5G portable Wi-Fi U50S is now officially on sale, starting at 899 yuan. In terms of appearance design, ZTE U50S Portable Wi-Fi is simple and stylish, easy to hold and pack. Its size is 159/73/18mm and is easy to carry, allowing you to enjoy 5G high-speed network anytime and anywhere, achieving an unimpeded mobile office and entertainment experience. ZTE 5G portable Wi-Fi U50S supports the advanced Wi-Fi 6 protocol with a peak rate of up to 1800Mbps. It relies on the Snapdragon X55 high-performance 5G platform to provide users with an extremely fast network experience. Not only does it support the 5G dual-mode SA+NSA network environment and Sub-6GHz frequency band, the measured network speed can even reach an astonishing 500Mbps, which is easily satisfactory.

According to news on July 12, the Honor Magic V3 series was officially released today, equipped with the new Honor Vision Soothing Oasis eye protection screen. While the screen itself has high specifications and high quality, it also pioneered the introduction of AI active eye protection technology. It is reported that the traditional way to alleviate myopia is "myopia glasses". The power of myopia glasses is evenly distributed to ensure that the central area of sight is imaged on the retina, but the peripheral area is imaged behind the retina. The retina senses that the image is behind, promoting the eye axis direction. grow later, thereby deepening the degree. At present, one of the main ways to alleviate the development of myopia is the "defocus lens". The central area has a normal power, and the peripheral area is adjusted through optical design partitions, so that the image in the peripheral area falls in front of the retina.

According to news on April 3, Taipower’s upcoming M50 Mini tablet computer is a device with rich functions and powerful performance. This new 8-inch small tablet is equipped with an 8.7-inch IPS screen, providing users with an excellent visual experience. Its metal body design is not only beautiful but also enhances the durability of the device. In terms of performance, the M50Mini is equipped with the Unisoc T606 eight-core processor, which has two A75 cores and six A55 cores, ensuring a smooth and efficient running experience. At the same time, the tablet is also equipped with a 6GB+128GB storage solution and supports 8GB memory expansion, which meets users’ needs for storage and multi-tasking. In terms of battery life, M50Mini is equipped with a 5000mAh battery and supports Ty

At work, ppt is an office software often used by professionals. A complete ppt must have a good ending page. Different professional requirements give different ppt production characteristics. Regarding the production of the end page, how can we design it more attractively? Let’s take a look at how to design the end page of ppt! The design of the ppt end page can be adjusted in terms of text and animation, and you can choose a simple or dazzling style according to your needs. Next, we will focus on how to use innovative expression methods to create a ppt end page that meets the requirements. So let’s start today’s tutorial. 1. For the production of the end page, any text in the picture can be used. The important thing about the end page is that it means that my presentation is over. 2. In addition to these words,

According to news on July 29, the Honor X60i mobile phone is officially on sale today, starting at 1,399 yuan. In terms of design, the Honor X60i mobile phone adopts a straight screen design with a hole in the center and almost unbounded ultra-narrow borders on all four sides, which greatly broadens the field of view. Honor X60i parameters Display: 6.7-inch high-definition display Battery: 5000mAh large-capacity battery Processor: Dimensity 6080 processor (TSMC 6nm, 2x2.4G A76+6×2G A55) System: MagicOS8.0 system Other features: 5G signal enhancement, smart capsule, under-screen fingerprint, dual MIC, noise reduction, knowledge Q&A, photography capabilities: rear dual camera system: 50 million pixels main camera, 2 million pixels auxiliary lens, front selfie lens: 8 million pixels, price: 8GB

Title: In-depth discussion of the importance and examples of establishing link files in Linux. In the Linux operating system, link files are a very useful concept. It can help users better organize and manage data in the file system and improve file accessibility. Accessibility and flexibility. Understanding how to create link files in Linux is crucial for system administrators and developers. This article will delve into the importance of establishing link files in Linux and demonstrate its usage and role through specific code examples. 1.What is
