我正在构建一个论坛页面,这是我的第一个PHP项目。我已经成功地连接了我的表并将值存储在PHPMYADMIN中,但它们没有显示在我的主论坛页面上,相反,显示的行会增加,但它们会存储空值,有什么建议吗?
<?php
include ('includes/session.php');
include ('includes/header.php');
$host = "localhost";
$username = "fses16g6";
$password = "fses16g6";
$db_name="fses16g6"; // Database name
$tbl_name="forum_question"; // Table name
// 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="#FFFFFF">
<tr>
<td width="6%" align="center" bgcolor="#000000"><strong>#</strong></td>
<td width="53%" align="center" bgcolor="#000000"><strong>Topic</strong></td>
<td width="15%" align="center" bgcolor="#000000"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#000000"><strong>Replies</strong> </td>
<td width="13%" align="center" bgcolor="#000000"><strong>Date/Time</strong> </td>
</tr>
<?php
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="#black"><a href="create_topic.php"> <strong>Create New Topic</strong> </a></td>
</tr>
</table>
<?php
//if ($is_user) {
//echo 'Welcome to Gamers Paradise'
//else echo 'You must be logged in to post.'
//}
if ($is_admin) {
echo '<button type="button">EDIT</button>';
echo '<button type="button">DELETE</button>';
}
include ('includes/footer.html');
?>
这是的桌子
CREATE TABLE `forum_question`
(`id` int(4) NOT NULL auto_increment,`topic` varchar(255) NOT NULL default'',
`detail` longtext NOT NULL,
`name` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
`datetime` varchar(25) NOT NULL default '',
`view` int(4) NOT NULL default '0',
`reply` int(4) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
更改此行:
while($rows=mysql_fetch_array($result)){
到。。。
while($rows=mysql_fetch_assoc($result)){
您正试图将结果作为关联数组访问,因此还需要将它们作为关联数组从数据库中获取。
但更重要的是,您需要停止使用mysql_*
函数。它们被弃用,而且不安全。
正如Dagon和matt kent所指出的,我的echo标记缺少打开的php分界,这导致没有显示任何值。
所以,不是:
<td align="center" bgcolor="#FFFFFF"><?echo $rows['view']; ?></td>
我需要:
<td align="center" bgcolor="#FFFFFF"><php?echo $rows['view']; ?></td>
这对所有的行都执行了此操作,但它们不显示。
感谢大家的帮助。