2011-03-02 146 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