0

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

我在下面发布了我的代码

添加.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 条目,但仍然是顶部的最新条目

4

1 回答 1

1

你有这行两次:

$rows=mysql_fetch_array($result);

摆脱第一个,只需使用:

while($row = mysql_fetch_array($result)){
    echo $row['message']
}
于 2012-10-22T15:36:52.363 回答