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:Copy codeThe 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.Copy codeThe Code is as follows: <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:Copy codeThe Code is as follows: <?
Setcookie ("nick", $ nick) // uses 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:Copy codeThe Code is as follows: <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. phpCopy codeThe Code is as follows: <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.