2014-04-23 10 views
0

我正在嘗試查看此表中的論壇主題。但是表格的內容不會顯示。我不知道我的代碼中缺少什麼。它顯示錶格並顯示正確數量的行但沒有文本。無法在PHP中查看我的主論壇表格的內容

<?php 
include "db.php" ?> 

$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 


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

<?php 
// Exit looping and close connection 
} 
mysql_close(); 
?> 

<tr> 
<td colspan="5" align="right" bgcolor="#E6E6E6"><a href="create_topic.php"><strong>Create New Topic</strong> </a></td> 
</tr> 
</table> 
+0

對您的$ result變量執行var_dump並向我們顯示結果 – AllisonC

+0

您確定該列在表中名爲'topic'嗎? – Anthony

回答

0

這可能是因爲你的服務器沒有啓用short_open_tag配置中刪除?>

因此,以下行不會被解析爲PHP代碼,而是被視爲純文本。

<? echo $rows['view']; ?> 

使用這個代替:

<?php echo $rows['view']; ?> 

短標籤被認爲是一個PITA在編碼世界,因爲如果你有你的代碼遷移到服務器不支持它,或者你無法啓用它,比只有在使用普通標籤時才能編輯所有代碼才適用於該環境。 但是,從5.4.x開始,短手標記由默認啓用並在任何PHP文檔中解析。但要記住不同的環境是很好的。

0

您的代碼:

<?php 
include "db.php" ?> 

// -------------------------- ^刪除此。

所以最終代碼應該是這樣的:

<?php 
include "db.php" 

$sql="SELECT * FROM $tbl_name ORDER BY id DESC"; 
// OREDER BY id DESC is order result by descending 

$result=mysql_query($sql); 
?> 
+0

謝謝@Sukhwinder鰓它工作! – user3560754

2

include "db.php"

<?php 
include "db.php" 

$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 


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

<?php 
// Exit looping and close connection 
} 
mysql_close(); 
?> 

<tr> 
<td colspan="5" align="right" bgcolor="#E6E6E6"><a href="create_topic.php"><strong>Create New Topic</strong> </a></td> 
</tr> 
</table>