MyBB Community Forums

Full Version: sql query to get latest threads
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there, I'm designing my site at moment, and I'm trying to come up with a script to get the latest 5 threads. Or maybe 10 or maybe 15. Can anybody help me out with what tables and groups I should be selecting? I'm having a look at http://docs.mybb.com/Database_Tables.html
I assume the "mybb_threads" table is where I want to be looking.

so I'm thinking I have to do something like:

 SELECT subject From mybb_threads WHERE <something something something> 

I'm disabled in that I know next to nothing about MYSql and database schema. Can anybody help me out?
Hi frodom, welcome to MyBB Smile
Are you trying to show the latest threads on your site outside of MyBB, or on a MyBB page?

These steps below should work "outside of MyBB".
There is an easier way and a better way.

First, the easier way:
  • Follow steps 1 and 2 here [at this link], and use the code below, instead of step 3.

<?php
// show recent posts
      $query = $db->query("
            SELECT t.*, u.username
            FROM mybb_threads t
            LEFT JOIN mybb_users u ON (u.uid=t.uid)
            WHERE 1=1 AND t.visible='1' AND t.closed NOT LIKE 'moved|%'
            ORDER BY t.lastpost DESC
            LIMIT 0, 20" // Change the last digit to how many recent post you want to be shown
        );

    $list = '';
     while($fetch = $db->fetch_array($query))
    {
// The next comment assumes this page is at the domain root...
// find "forums" (twice) and replace with the directory name of your forum
        $list .= "<p class=\"big\"><strong><a href=\"forums/showthread.php?tid={$fetch['tid']}\">".htmlspecialchars_uni($fetch['subject'])."</a></strong><br />";
        $poster = "<a href=\"forums/member.php?action=profile&uid=".$fetch['uid']."\">{$fetch['username']}</a>";
        $list .= "Created by: {$poster}<br />";
        $list .= "<i>" .$fetch['replies']. " Replies</i>";
        $list .= "<i> , " .$fetch['views']. " Views</i><br />";
        $list .= " (<i>Last post by: " .$fetch['lastposter']. "</i>)<br /><hr width=\"50\"></p>";   
    }
    echo $list;
?>
http://community.mybb.com/thread-62787.html
original source (for MyBB 1.4), the example above is modified to work with the current 1.6.x



#
Now, the better* way:
This code, http://community.mybb.com/thread-110658-...#pid805233 , needs to be modified to work outside of MyBB it is currently written for "internal use".

*Better?
It will eliminate titles for threads/forums users cannot view.
Thanking you, seeker. Yeah what I was trying to do was display the latest threads on my main site. I've made things very difficult for myself by having the main site running on asp.net. So I had to use this awkward adapter class to get into the mysqldb. I've got it working now though, with the query you've shown, but using C# instead of php.

What I'm wondering now is maybe I should just use php like you have above to generate an RSS feed or plain xml of the latest posts instead, which I can easily consume from asp.net. Seems like a much cleaner approach to me. Hmmm.
You are welcome...
asp.net?
Sounds like a project for your company?
No it's a personal project. I just use asp.net because C# is the language I know. I can run php on the windows server though.