Home >Backend Development >PHP Tutorial >Writing a chat room with PHP+MySql_PHP tutorial
Hello everyone! I haven’t seen you all for a long time. Last time I wrote about a chat room that uses PHP to operate through files. Of course it is full of loopholes, and every time I refresh the screen it is terrible! I have been thinking about it for a long time. Is there a way to get the speech information without refreshing? This effect can be achieved by using one more framework. That is, using one more framework is called getmsg. We let getmeg obtain information and ensure that the information obtained each time is the latest. That is, the speech obtained last time is not needed. Last time I edited The problem is that every time the information is obtained, it is repeated, which is not effective.
My design idea is: for every speech, submit it to the server and then save it to a database. Getmsg will go to the server to obtain the latest speech data every few seconds, and ensure the last speech obtained. Don't remove it, and then superimpose the removed speech into a window (listmsg) dedicated to displaying speeches. In this way, we feel that the listmsg window will not have to be refreshed. Moreover, we can only see the speech information being added continuously, which has a very good effect.
How to add information from one frame to another frame? This idea can be realized using JavaScript:
Of course you have to build two tables in mysql, one table is used to save speech information, and the other table is used to save several online ones. When counting the number of people, we let it refresh every 60 seconds, delete the speeches made one minute ago, and see if a certain speaker has not spoken for a long time. If so, delete him.
Due to The subsections of the table are very simple, so there is no need to write them out here. You will know them by looking at the program.This program works very well both online and offline!
表的名字是:chat和chat_getmsg
if(!isset($username))
$username="guest";
$conid=mysql_connect("localhost","yourcounter","password");
mysql_select_db("yourdadabase",$conid);
$dstr=date("YmdHis");
$sql="insert chat_getmsg (username,shijian) values ('$username','$dstr')";
mysql_query($sql,$conid);
$sql="update chat_getmsg set shijian='$dstr' where username='$username'";
mysql_query($sql,$conid);
mysql_close($conid);
?>