0

So I have this PHP code:

  • Note: I do use mysqli_connect() further up.

    $result = mysqli_query($con,"SELECT * FROM `smf_messages` WHERE `id_board` = 18");
    if(!$result) {
        echo "<center><p>Couldn't fetch news posts. Error code 2.</p></center>";
        mysqli_close($con);
    } else {
        $posts = array();
        $topicbdy = array();
        while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
        {
            $posts[$row['id_topic']] = $row['id_topic'];
            $topicbdy[$row['id_msg']] = $row['id_msg'];
        }
        $display = max($posts);
        $display2 = min($topicbdy);
        $qry = "SELECT * FROM `smf_messages` WHERE `id_board` = 18 AND `id_topic` = " . $display . " AND `id_msg` = " . $display2;
        $result2 = mysqli_query($con,$qry);
        //echo $qry;
        if(!$result2) {
            echo "<center><p>Couldn't fetch news posts. Error code 3.</p></center>";
        } else {
            while($show = mysqli_fetch_array($result,MYSQLI_ASSOC))
            {
                echo "<center><h1>" . $show['subject'] . "</h1></center><br /><br />";
                echo "<center>" . $show['body'] . "</center><br />";
            }
        }
        mysqli_free_result($result);
        mysqli_free_result($result2);
        mysqli_close($con);
    

It's supposed to get the latest topic out of the database for my SMF-based forum from the news board, by getting the highest topic id, but the lowest post id. It seems to be doing the query just fine, as I don't get any errors, but it doesn't show the subject or body. What should I do?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

1

Your $result variable is wrong for second query fetch. For your second query

 while($show = mysqli_fetch_array($result,MYSQLI_ASSOC))

Should be

 while($show = mysqli_fetch_array($result2,MYSQLI_ASSOC))
                                         ^
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95