MySQL结果插入数据库,但不显示任何结果


MySQL results inserting into database but not displaying any results

正如标题所示,我收到了"主题无法显示,请稍后再试。"错误消息,但数据插入到我的mysql表中很好,但我不明白为什么它们没有显示,这是我的结果页面。

<?php
include 'core/init.php';
include 'includes/overall/header.php';
$sql = "SELECT 'topic_id', 'topic_subject', 'category', 'sub_category', 'posted_by', 'posted', 'view', 'reply' FROM `topics`
    WHERE topics.topic_id = " . mysql_real_escape_string($_GET['id']);
$result = mysql_query($sql);
if(!$result)
{
echo 'The topic could not be displayed, please try again later.';
}
else
{
if(mysql_num_rows($result) == 0)
{
    echo 'This topic doesn&prime;t exist.';
}
else
{
    while($row = mysql_fetch_assoc($result))
    {
        $posts_sql = "SELECT 'posts.post_topic', 'posts.post_content', 'posts.post_date', 'posts.post_by', 
                             'users.user_id', 'users.username', 'users.profile'
                FROM
                    `posts`
                LEFT JOIN
                    `users`
                ON
                    posts.post_by = users.user_id
                WHERE
                    posts.post_topic = " . mysql_real_escape_string($_GET['id']);
        $posts_result = mysql_query($posts_sql);
        if(!$posts_result)
        {
            echo '<tr><td>The posts could not be displayed, please try again later.</tr></td></table>';
        }
        else
        {
            while($posts_row = mysql_fetch_assoc($posts_result))
            {
                include("includes/results-template.php");
            }
        }
        if(!$_SESSION['signed_in'])
        {
            echo '<tr><td colspan=2>You must be <a href="signin.php">signed in</a> to reply. You can also <a href="signup.php">sign up</a> for an account.';
        }
        else
        {
            //show reply box
            echo '<tr><td colspan="2"><h2>Reply:</h2><br />
                <form method="post" action="reply.php?id=' . $row['topic_id'] . '">
                    <textarea name="reply-content"></textarea><br /><br />
                    <input type="submit" value="Submit reply" />
                </form></td></tr>';
        }
        //finish the table
        echo '</table>';
    }
}
}
?>
<?php include 'includes/overall/footer.php'; ?>

您不能选择带单引号的列,而应该选择带反引号的列。

将查询更改为

    $sql = "SELECT `topic_id`, `topic_subject`, `category`, `sub_category`, `posted_by`, `posted`, `view`, `reply`  FROM `topics`
WHERE topics.topic_id =  '".mysql_real_escape_string($_GET['id'])."' ";

并将该查询$posts_sql更改为:

        $posts_sql = "SELECT posts.post_topic, posts.post_content, posts.post_date, posts.post_by, users.user_id, users.username, users.profile
            FROM
                `posts`
            LEFT JOIN
                `users`
            ON
                posts.post_by = users.user_id
            WHERE
                posts.post_topic = '" . mysql_real_escape_string($_GET['id'])."' ";

反引号和单引号之间有区别。