2011-03-02 142 views
0

這是我用來查看論壇內容的簡單php代碼。問題是,它在我的一臺筆記本電腦上工作正常,但在第二次它不顯示輸出。這個php代碼有什麼問題?

// Connect to server and select databse. 
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"; 
// OREDER BY id DESC is order result by descending 
$result=mysql_query($sql); 
?> 
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> 
<tr> 
<td width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td> 
<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td> 
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td> 
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td> 
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td> 
</tr> 

<?php 
while($rows=mysql_fetch_array($result)){ // Start looping table row 
?> 
<tr> 
<td bgcolor="#E6E6E6"><? echo $rows['id']; ?></td> 
<td bgcolor="#E6E6E6"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['topic']; ?></a><BR></td> 
<td align="center" bgcolor="#E6E6E6"><? echo $rows['view']; ?></td> 
<td align="center" bgcolor="#E6E6E6"><? echo $rows['reply']; ?></td> 
<td align="center" bgcolor="#E6E6E6"><? echo $rows['datetime']; ?></td> 
</tr> 

我檢查了一切,他們似乎很好。數據存在於數據庫中,但未在論壇中顯示。有人可以幫我嗎? 操作系統:win7

+0

請發佈您的所有代碼和數據庫結構。 – 2011-03-02 08:49:09

+0

我看不到'$ tbl_name'在哪裏定義。您是在筆記本電腦上運行此代碼還是在其他位置託管? – 2011-03-02 08:49:11

+1

也許短標籤被禁用。更改<? echo $ rows ['id']; ?>到<?php echo $ rows ['id']; ?>和所有以<?開頭的行。到<?php – smottt 2011-03-02 08:49:25

回答

2

學習調試的時間,朋友。

良好的開端:http://www.ibm.com/developerworks/library/os-debug/

爲MySQL這是非常方便的運行查詢是這樣的:

$result=mysql_query($sql) or trigger_error(mysql_error()." ".$sql); 

,並確保你可以看到發生的所有錯誤。 如果你不能爲速戰速決,你可以在腳本

ini_set('display_errors',1); 
error_reporting(E_ALL); 

的頂部添加這些行,但用於生產狀態display_errors值改爲0

這也是很好的做法檢查HTML源代碼,而不是在瀏覽器中觀看渲染頁面。它會告訴你,如果你有短標籤或沒有任何問題。在解決問題之前知道您是否有任何問題總是很好的。

2

你應該使用

<?php echo $rows['id']; ?>而不是<? echo $rows['id']; ?>

短標籤可能被禁用。

+0

感謝哥們。完成。 – shona 2011-03-02 08:53:36