Php mysqli fetch_object()致命错误

Php mysqli fetch_object()致命错误,php,mysqli,Php,Mysqli,我尝试在我的论坛数据库中使用mysqli。这是我使用的代码: <meta charset="utf-8"> <?php include("config.php"); $limits = "6"; $forum_id = "2"; $db = new mysqli($INFO['sql_host'], $INFO['sql_user'], $INFO['sql_pass'], $INFO['sql_database']); $topics = $db->quer

我尝试在我的论坛数据库中使用mysqli。这是我使用的代码:

<meta charset="utf-8">
<?php


include("config.php");
$limits = "6";
$forum_id = "2"; 

$db = new mysqli($INFO['sql_host'], $INFO['sql_user'], $INFO['sql_pass'], $INFO['sql_database']);


$topics = $db->query("
    SELECT
        `topics`.`start_date`,
        `topics`.`title`,
        `topics`.`starter_name`,
        `topics`.`posts`,
        `topics`.`title_seo`,
        `topics`.`tid`,
        `posts`.`post`
    FROM
        `" . $INFO['sql_tbl_prefix'] . "topics` as `topic`,
        `" . $INFO['sql_tbl_prefix'] . "posts` as `post`
    WHERE
        `topics`.`approved` = 1 AND
        `topics`.`forum_id`= " . $forum_id . " AND
        `posts`.`topic_id` = `topic`.`tid` AND
        `posts`.`new_topic` = 1
    ORDER BY
        `topics`.`start_date`
    DESC LIMIT 5");

echo '<ul id="news">';

while ($topic = $topics->fetch_object()) {


    $url = $INFO['board_url'] . '/index.php?/topic/' . $topic->tid . '-' . $topic->title_seo . '/';


    $topic->post = strip_tags(str_replace(array('[', ']'), array('<', '>'), $topic->post));


    $topic->start_date = date("Y.m.d H:i", $topic->start_date);

    echo '
<div class="news">
<div class="newsp"><div class="pteksts"><a href="'.$url.'">' . $topic->title . '</div></div></a>
<center><img src="img/news.png"></center>
<div class="teksts" style="padding-bottom: 5px;">' . $topic->post . '</div>
</div>

';
}

echo '</ul>';
?>


您将表的别名指定为
topic
post
,但随后使用别名
topics
posts
。您需要更改表限定符以使用与表别名相同的拼写

错误,因为别名
主题
与表限定符
主题
不同:

SELECT
    `topics`.`start_date`, . . .
FROM
    `" . $INFO['sql_tbl_prefix'] . "topics` as `topic`,
. . .
SELECT
    `topic`.`start_date`, . . .
FROM
    `" . $INFO['sql_tbl_prefix'] . "topics` as `topic`,
. . .
SELECT
    `topics`.`start_date`, . . .
FROM
    `" . $INFO['sql_tbl_prefix'] . "topics` as `topics`,
. . .
正确,更改表限定符以匹配别名后:

SELECT
    `topics`.`start_date`, . . .
FROM
    `" . $INFO['sql_tbl_prefix'] . "topics` as `topic`,
. . .
SELECT
    `topic`.`start_date`, . . .
FROM
    `" . $INFO['sql_tbl_prefix'] . "topics` as `topic`,
. . .
SELECT
    `topics`.`start_date`, . . .
FROM
    `" . $INFO['sql_tbl_prefix'] . "topics` as `topics`,
. . .
也正确,但如果别名与基表名称相同,则不需要别名:

SELECT
    `topics`.`start_date`, . . .
FROM
    `" . $INFO['sql_tbl_prefix'] . "topics` as `topic`,
. . .
SELECT
    `topic`.`start_date`, . . .
FROM
    `" . $INFO['sql_tbl_prefix'] . "topics` as `topic`,
. . .
SELECT
    `topics`.`start_date`, . . .
FROM
    `" . $INFO['sql_tbl_prefix'] . "topics` as `topics`,
. . .
但更重要的是,您应该始终检查$db->query()中的返回值,因为如果出现错误,它将返回false。不能对false调用任何方法,因为它不是对象

如果发生这种情况,请报告错误,但不要尝试从结果中提取。这行不通

$topics = $db->query(...);
if ($topics === false) {
    die($db->error);
}

// now we can be sure it's safe to call methods on $topics
while ($topic = $topics->fetch_object()) {
    . . .

请重新评论输出为空:

我刚刚测试了这个脚本,它基本上是有效的,所以我猜不出哪里出了问题。我建议您阅读http服务器的错误日志,这是许多PHP通知和错误输出的地方

我确实看到以下通知:

Notice: A non well formed numeric value encountered in /Users/billkarwin/workspace/SQL/22159646.php on line 51
这条线是这样的:

$topic->start_date = date("Y.m.d H:i", $topic->start_date);
问题是PHP函数采用整数时间戳,而不是日期字符串


您可能希望使用MySQL函数在SQL中格式化日期。

现在它在“字段列表”中显示未知列“主题”。那么,“开始日期”听起来您可能也拼错了列名。检查表中使用了哪些列名以及
SHOW CREATE table prefix\u topics
(其中prefix是
$INFO['sql\u tbl\u prefix']
的任何内容)。该表没有前缀,因此我删除了$INFO['sql\u tbl\u prefix']仍然显示相同的错误并且我检查的表格拼写正确是
start\u date
您的
主题
表格中的一列还是
帖子
表格?
start\u date
是表格
主题中的一列