Question

I tried to use mysqli in for my forum database. this is the code I used:

<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>';
?>

and errors i received:

Fatal error: Call to a member function fetch_object() on a non-object in /home/public_html/scripts/news.php on line 35

Was it helpful?

Solution

You give aliases for your tables as topic and post, but then you use the aliases topics and posts. You need to change the table qualifiers to use the same spelling as your table alias.

Wrong, because alias topic is not the same as table qualifier topics:

SELECT
    `topics`.`start_date`, . . .
FROM
    `" . $INFO['sql_tbl_prefix'] . "topics` as `topic`,
. . .

Right, after changing the table qualifier to match the alias name:

SELECT
    `topic`.`start_date`, . . .
FROM
    `" . $INFO['sql_tbl_prefix'] . "topics` as `topic`,
. . .

Right as well, but alias is unnecessary if it's the same as the base table name:

SELECT
    `topics`.`start_date`, . . .
FROM
    `" . $INFO['sql_tbl_prefix'] . "topics` as `topics`,
. . .

But more to the point, you should always check the return value from $db->query(), because it returns false if there's an error. You can't call any method on a false because that's not an object.

If that happens, report the error but do not try to fetch from the result. It won't work.

$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()) {
    . . .

Re your comment that the output is blank:

I just tested this script and it mostly works, so I can't guess what's going wrong. I suggest you read your http server's error log, which is where many PHP notices and errors are output.

I do see the following notice:

Notice: A non well formed numeric value encountered in /Users/billkarwin/workspace/SQL/22159646.php on line 51

The line is this:

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

The problem is that PHP's date() function takes an integer timestamp, not a date string.

You might want to format the date in SQL, using MySQL DATE_FORMAT() function instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top