2012-10-22 41 views
0

我的工作我的聊天腳本,但是我只拿到了第PHP SQL僅每第二個條目

我已張貼下面

Add.php代碼我的代碼在每一秒SQL入門:

<?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

您此行兩次:

$rows=mysql_fetch_array($result); 

擺脫了第一個,只是使用:

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

除了'$ rows'不包含在完成所有行一個電話。它只包含_one_行(第一個)。 'mysql_fetch_array()'必須在循環中調用以累積到'$ rows []' –

+1

應該完全消除第一次調用mysql_fetch_array()。 –

+0

哎呀,是的,謝謝@MichaelBerkowski – CAMason