初学者需要有关sql和php的帮助

初学者需要有关sql和php的帮助

我正在为我的网站制作一个简单的论坛,但我有一些问题
它不会打印帖子。我没有错误非常奇怪。它打印线程标题。

$id = isset($_GET['t']) ? intval($_GET['t']) : 0;
$query = "SELECT * FROM threads t INNER JOIN posts p ON p.tid = t.id WHERE t.id = $id";
$result = mysql_query($query);

// see if thread exists
if (!mysql_num_rows($result)) {
    echo "The requested thread does not exist.";
    return false;
}

// Fetch the rows
$row = mysql_fetch_assoc($result);

// Print title
echo '<h1>'.htmlspecialchars($row['title']).'</h1>';
// Print posts
while ($row2 = mysql_fetch_assoc($result)) {
    echo $row2['message'];
}

桌子:
CREATE TABLE IF NOT EXISTS `threads` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
)

INSERT INTO `threads` (`id`, `title`) VALUES
(1, 'My first thread!');

CREATE TABLE IF NOT EXISTS `posts` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`tid` int(10) unsigned NOT NULL DEFAULT '0',
`message` text NOT NULL,
PRIMARY KEY (`id`)
)

INSERT INTO `posts` (`id`, `tid`, `message`) VALUES
(1, 1, 'This is my first post in my first thread :)');

最佳答案

检查查询和数据库连接是否成功。你没有这样做,所以现在如果查询失败,它会默默地这样做,而你被困在黑暗中:

$result = mysql_query($query) or die(mysql_error());

在开发过程中这样的东西可以节省你很多的头发和时间。

关于php - 初学者需要有关sql和php的帮助,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4973398/

10-14 13:59