1. 最简单的方式——不断刷新页面
数据库建立
create table chat ( chattime datetime, nick char(10), words char(150) );
login.php
<title>用户登录</title> <meta charset="utf-8"> 请输入您的昵称<br>
<title>显示用户发言</title> <meta http-equiv="refresh" c> <?php $conn = mysql_connect("127.0.0.1", "root", "7940175"); mysql_select_db("yuema", $conn); $str = "select * from chat order by chattime;"; $result = mysql_query($str, $conn); $rows = mysql_num_rows($result); mysql_data_seek($result, $rows-15); //取最近插入的15条数据 if ($rows<15) $l = $rows; else $l = 15; for ($i = 1; $i <= $l; $i++) { //输出这15条数据 list($chattime, $nick, $words) = mysql_fetch_row($result); echo $chattime; echo " ".$nick." "; echo $words; echo "<br>"; } ?>结果展示
2. ajax获取,不刷新页面
以上就介绍了php+html实现聊天室,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。