我正在构建一个论坛页面,这是我的第一个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 ;
答案 0 :(得分:0)
更改此行:
while($rows=mysql_fetch_array($result)){
为...
while($rows=mysql_fetch_assoc($result)){
您尝试将结果作为关联数组进行访问,因此您还需要将其作为关联数组从数据库中获取。
但更重要的是,您需要停止使用mysql_*
个功能。他们被弃用并且不安全。
答案 1 :(得分:0)