Use PHP+MySQL to build the chat room function instance code

  • 2020-05-19 04:28:07
  • OfStack

Below, 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 the chat room, is to connect each user with the same 1 web page to transfer the speech data stored, and then all the speech data to each user. That is to say, collect everybody's speech with the database, pass the data in the database to every 1 person realized the function of chatroom.
1.2 table design
First, MySQL is used to create a table, chat, to store user statements:
 
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 the speech, nick is the nickname of the speaker, and words is the content of the speech, with a maximum of 150 characters
1.3 web design
A simple chat room usually requires two page boxes: one for the user to enter a speech form, and one for the user to display their speech. So code snippets usually need at least the following:
Establish the structure of the page frame (main.php)
Show the segment of the presentation (cdisplay.php)
Program segment to transmit user speech (speak.php)
User login into the chat room program segment (login.php)

2. Code design

After the above planning is completed, you can start to design the code. Using php can achieve the above functions in a very simple way.
2.1 user login login.php, this code is a full HTML page
 
<html> 
<head> 
 <title> The user login </title> 
</head> 
<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> 
</html> 

After the user submits his/her nickname, he/she enters the chat room and the following processing is handled by main.php.
2.2 page box body code segment main. php:
 
<? 
 setcookie( " nick " ,$nick) // with cookie Record user nicknames , Is a common method of passing variables  
?> 
<html> 
<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> 
</html> 

2.3 display speech cdisplay.php
The task of this code snippet is to fetch the data from the table chat and display it in the page box. At each refresh, take the last 15 statements in the database. At the same time, in order to prevent the infinite growth of the database, we need to design the function of deleting stale data. The following code
 
<html> 
<head> 
 <title> Display user speech </title> 
 <meta http-equiv= " refresh "  content= " 5;url=cdisplay.php " > 
</head> 
<body> 
<? 
 $link_ID=mysql_connect( " main " , " root " ); 
 // link Mysql The server   Server name main, Administrator name 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); // The number of pens used to obtain the query result  
 // In the final 15 Pen to speak and display  
 @mysql_data_seek($resut,$rows-15); // Move the record pointer forward 15 Pen to record  
 if ($rows<15) $l=$rows; else $l=15; // Total records less than 15 , is at most the 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> " ; 
 } 
 // Clean out the stale data in the library  
 @mysql_data_seek($result,$rows-20); // Move the record pointer forward 20 Pen to record  
 list($limtime)=mysql_fetch_row($result); 
 $str= " DELETE FROM chat WHERE chtime<'$limtime' ; "  ; 
 $result=mysql_query($str,$link_ID); // The query string is sent , Only the front of the library 20 A record  
 mysql_close($link_ID); 
?> 
</body> 
</html> 

2.4 send speeches to the database speak.php
 
<html> 
<head> 
 <title> To speak </title> 
</head> 
<body> 
<? 
 If ($words) 
  { $link_ID=mysql_connect( " main " , " root " ); 
  mysql_select_db( " abc " ); // Database name 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 speech to the database  
  mysql_close($link_ID); 
 } 
?> 
// Enter a form for speaking  
<form action= " speak.php "  method= " post "  target= "  _self " > 
 <input type= " text "  name= " words "  cols= " 20 " > 
 <input type= " submit "  value= "Speak" > 
</form> 
</body> 
</html> 

After finishing above work, 1 simple chat room is made finished. Of course, the designer can do 1 some personalized design according to the individual hobby, such as adding 1 page box, display the current chat room personnel list, increase the expression, get the speaker IP, step by step beautify the page and so on.

Related articles: