MyBB Community Forums

Full Version: Showing posts on external page not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to show posts from a specific forum on a regular php page from my website. I tried to use the code from the last post this topic: https://community.mybb.com/thread-210775.html with mediocre results.

While the posts get shown correctly like

Post Title
Post Content
Posted by: username

I end up with getting these errors displayed on the same page just before the posts:

MyBB Internal: One or more warnings occurred. Please contact your administrator for assistance.
MyBB Internal: One or more warnings occurred. Please contact your administrator for assistance.
MyBB Internal: One or more warnings occurred. Please contact your administrator for assistance.
MyBB Internal: One or more warnings occurred. Please contact your administrator for assistance.
MyBB Internal: One or more warnings occurred. Please contact your administrator for assistance.
MyBB Internal: One or more warnings occurred. Please contact your administrator for assistance.


This is the code I put in the first row of the file /test/showcontent.php

<?php
chdir("./../forum/"); // path to MyBB
define("IN_MYBB", 1);
require_once("./global.php");
global $mybb, $db, $cache, $lang, $plugins;  // Trim this list of global vars, if you want to.
?>


And this code is inserted in the same document at the place where the posts should be shown

<?php
$query = $db->query("
    SELECT t.tid, t.fid, t.uid, t.username, t.subject, t.replies, t.views, t.lastposter, p.message
    FROM ".customboardprefix_."threads t
    INNER JOIN ".customboardprefix_."posts p ON (p.tid=t.tid)
    WHERE t.fid='2' AND t.visible='1'
    ORDER BY t.dateline DESC
    LIMIT 6"
	
); 
$list = '';
    while($fetch = $db->fetch_array($query))
    {
        $list .= "<strong><a href=\"forum/showthread.php?tid={$fetch['tid']}\">".htmlspecialchars_uni($fetch['subject'])."</a></strong><br />";
		$list .= "".htmlspecialchars_uni($fetch['message'])."<br>";
        $poster = "<a href=\"forum/member.php?action=profile&uid=".$fetch['uid']."\">{$fetch['username']}</a>";
        $list .= "Posted by: {$poster}<br /><br />";
    
    }
    //output
    echo $list;
?>


The error.log shows this:

<error>
	<dateline>1610572442</dateline>
	<script>inc/functions.php</script>
	<line>4820</line>
	<type>2</type>
	<friendly_type>Warning</friendly_type>
	<message>Cannot modify header information - headers already sent by (output started at /www/htdocs/root_id/mydomain.com/test/showdocument.php:1)</message>
	<back_trace>#0  errorHandler->error() called at [[PHP]: ]
#1  header() called at [/inc/functions.php:4820]
#2  send_page_headers() called at [/global.php:37]
#3  require_once() called at [/www/htdocs/root_id/mydomain.com/test/showdocument.php:29]
</back_trace>
</error>

<error>
	<dateline>1610572442</dateline>
	<script>inc/functions.php</script>
	<line>2063</line>
	<type>2</type>
	<friendly_type>Warning</friendly_type>
	<message>Cannot modify header information - headers already sent by (output started at /www/htdocs/root_id/mydomain.com/test/showdocument.php:1)</message>
	<back_trace>#0  errorHandler->error() called at [[PHP]: ]
#1  header() called at [/inc/functions.php:2063]
#2  my_setcookie() called at [/inc/class_session.php:380]
#3  session->load_guest() called at [/inc/class_session.php:113]
#4  session->init() called at [/global.php:49]
#5  require_once() called at [/www/htdocs/root_id/mydomain.com/test/showdocument.php:29]
</back_trace>
</error>

<error>
	<dateline>1610572442</dateline>
	<script>inc/functions.php</script>
	<line>2063</line>
	<type>2</type>
	<friendly_type>Warning</friendly_type>
	<message>Cannot modify header information - headers already sent by (output started at /www/htdocs/root_id/mydomain.com/test/showdocument.php:1)</message>
	<back_trace>#0  errorHandler->error() called at [[PHP]: ]
#1  header() called at [/inc/functions.php:2063]
#2  my_setcookie() called at [/inc/class_session.php:385]
#3  session->load_guest() called at [/inc/class_session.php:113]
#4  session->init() called at [/global.php:49]
#5  require_once() called at [/www/htdocs/root_id/mydomain.com/test/showdocument.php:29]
</back_trace>
</error>

<error>
	<dateline>1610572442</dateline>
	<script>inc/functions.php</script>
	<line>2063</line>
	<type>2</type>
	<friendly_type>Warning</friendly_type>
	<message>Cannot modify header information - headers already sent by (output started at /www/htdocs/root_id/mydomain.com/test/showdocument.php:1)</message>
	<back_trace>#0  errorHandler->error() called at [[PHP]: ]
#1  header() called at [/inc/functions.php:2063]
#2  my_setcookie() called at [/inc/class_session.php:120]
#3  session->init() called at [/global.php:49]
#4  require_once() called at [/www/htdocs/root_id/mydomain.com/test/showdocument.php:29]
</back_trace>
</error>

<error>
	<dateline>1610572442</dateline>
	<script>/www/htdocs/root_id/mydomain.com/test/showdocument.php</script>
	<line>37</line>
	<type>2</type>
	<friendly_type>Warning</friendly_type>
	<message>Use of undefined constant customboardprefix_ - assumed 'customboardprefix_' (this will throw an Error in a future version of PHP)</message>
	<back_trace>#0  errorHandler->error() called at [/www/htdocs/root_id/mydomain.com/test/showdocument.php:37]
</back_trace>
</error>

<error>
	<dateline>1610572442</dateline>
	<script>/www/htdocs/root_id/mydomain.com/test/showdocument.php</script>
	<line>38</line>
	<type>2</type>
	<friendly_type>Warning</friendly_type>
	<message>Use of undefined constant customboardprefix_ - assumed 'customboardprefix_' (this will throw an Error in a future version of PHP)</message>
	<back_trace>#0  errorHandler->error() called at [/www/htdocs/root_id/mydomain.com/test/showdocument.php:38]
</back_trace>
</error>

I assume customboardprefix_ is self explanatory to the people with enough knowledge to help me out on this one.

Really looking forward to get this fixed since mybb is such a nice board compared to phpbb. I'm impressed.

Thank you for your time, I really appreciate everybody who is trying to help me
Headers already sent means the script has already started to send data to the client and mybb is trying to set a new header ... move the code to somewhere before output starts
Thanks for your reply but the problem still persists.

I already had the require path at the very beinning. Even if I put every line of the require and query part at the very beginning of the file I still get the headers already sent errors. I even have disabled any https / htacess forwarding/forcing of SSL which had inflicted some problems on another website of mine.

I just don't get it.

Beside the headers already sent problem, why does this error occur:

<message>Use of undefined constant customboardprefix_ - assumed 'customboardprefix_' (this will throw an Error in a future version of PHP)</message>
Course it is undefined  check the define for a different spelling or case
The trouble is that you (or someone else) had replaced the constant (TABLE_PREFIX) with an hardcoded value without changing the quoting.

As you use the MyBB tables, your code must be:
$query = $db->query("
    SELECT t.tid, t.fid, t.uid, t.username, t.subject, t.replies, t.views, t.lastposter, p.message
    FROM ".TABLE_PREFIX."threads t
    INNER JOIN ".TABLE_PREFIX."posts p ON (p.tid=t.tid)
    WHERE t.fid='2' AND t.visible='1'
    ORDER BY t.dateline DESC
    LIMIT 6"
);

If you really want to hardcode the tablename (deprecated):
$query = $db->query("
    SELECT t.tid, t.fid, t.uid, t.username, t.subject, t.replies, t.views, t.lastposter, p.message
    FROM customboardprefix_threads t
    INNER JOIN customboardprefix_posts p ON (p.tid=t.tid)
    WHERE t.fid='2' AND t.visible='1'
    ORDER BY t.dateline DESC
    LIMIT 6"
);