PHP SQL只是每隔一个条目

时间:2012-10-22 15:35:30

标签: php mysql sql chat

我正在处理我的聊天脚本,但是我只在页面上获得每一秒的SQL条目

我已在下面发布了我的代码

Add.php代码:

<?php
mysql_connect( 'localhost:/tmp/mysql.sock', 'xxxxx', 'xxxxx' );
mysql_select_db( 'ios' );
mysql_query( "INSERT INTO chatitems VALUES ( null, null, '".mysql_real_escape_string( $_GET['user'] )."', '".mysql_real_escape_string( $_GET['message'] ). "')" );
header("Location: messages.php");
?>

messages.php代码:

<?php
$host="localhost:/tmp/mysql.sock";
$username="xxxxx";
$password="xxxxx";
$db_name="ios";
$tbl_name="chatitems";

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="yes" name="apple-mobile-web-app-capable" />
<meta content="index,follow" name="robots" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<link href="pics/homescreen.gif" rel="apple-touch-icon" />
<meta content="minimum-scale=1.0, width=device-width, maximum-scale=0.6667, user-scalable=no" name="viewport" />
<link href="css/style.css" rel="stylesheet" media="screen" type="text/css" />
<script src="javascript/functions.js" type="text/javascript"></script>
<title>VT Chat
</title>
<link href="pics/startup.png" rel="apple-touch-startup-image" />
</head>
<body>
<p>
<div id="content">
<?php
while($rows=mysql_fetch_array($result)){ 
?>
<ul class="pageitem">
<li class="textbox"><span class="header"><? echo $rows['user']; ?></span><p>
<? echo $rows['message']; ?></p></li>
<li class="pageitem"><span class="header"><? echo $rows['added']; ?></span></li>
</ul><p>
<?
}
mysql_close();
?>  
</div>
</body>
</html>

所以我只需要显示每个SQL条目,但仍然是最新的

1 个答案:

答案 0 :(得分:1)

你有两次这个行:

$rows=mysql_fetch_array($result);

摆脱第一个并使用:

while($row = mysql_fetch_array($result)){
    echo $row['message']
}