MyBB Community Forums

Full Version: Custom Function in SDK
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've tried adding my own function in sdk.php to read the subject and message from a hidden forum on my site. The function is:
	function getNews()
	    {
			$query = $this->db->query("SELECT subject,message FROM ".TABLE_PREFIX."posts WHERE fid='14' ORDER BY 'dateline'");
			while($newsItem = $db->fetch_array($query))
				{
					$news[$count] = $newsItem;
					$count++;
				}
			return $news;
 		}
and i get the following error
Fatal error: Call to a member function on a non-object in /home/mpsounds/public_html/forums/inc/sdk.php on line 212
where line 212 is
while($newsItem = $db->fetch_array($query))
function getNews(){
global $db;
$query = $db->query(...);
oh yea i forgot to say that before i put the while loop in, i just had
$news = $db->fetch_array($query)
return $news
and it worked fine
decswxaqz Wrote:function getNews(){
global $db;
$query = $db->query(...);
Ive tried that and now i get
Parse error: parse error, unexpected T_OBJECT_OPERATOR in /home/mpsounds/public_html/forums/inc/sdk.php on line 212

where line 212 is
$query = db->query("SELECT subject,message FROM ".TABLE_PREFIX."posts WHERE fid='14' ORDER BY 'dateline'");
You dont need global $db; (or atleast, not in myBB gold!). Try this
function getNews($fid) {
$query =  $this->$db->query("SELECT subject,message FROM ".TABLE_PREFIX."posts WHERE fid='$fid' ORDER BY 'dateline'");
while($newsItem =  $this->$db->fetch_array($query))
{
$news[$count] = $newsItem;
$count++;
}
return $news;
}
and
getNews(14)
i get
Fatal error: Call to a member function on a non-object in /home/mpsounds/public_html/forums/inc/sdk.php on line 223

Heres the code i have
function getNews($fid) {
$query =  $this->$db->query("SELECT subject,message FROM ".TABLE_PREFIX."posts WHERE fid='$fid' ORDER BY 'dateline'");
while($newsItem =  $this->$db->fetch_array($query))
{
$news[$count] = $newsItem;
$count++;
}
return $news;
}

line 223 is '$query = $this->$db->query(...)'
hmm, ok, try it with the other peice.
function getNews($fid) {
global $db;
$query = $this->$db->query("SELECT subject,message FROM ".TABLE_PREFIX."posts WHERE fid='$fid' ORDER BY 'dateline'");
while($newsItem = $this->$db->fetch_array($query))
{
$news[$count] = $newsItem;
$count++;
}
return $news;
}
still same error Sad

cheers for all the help btw
ah, thats why
function getNews($fid) {
global $db;
$query = $this->db->query("SELECT subject,message FROM ".TABLE_PREFIX."posts WHERE fid='$fid' ORDER BY 'dateline'");
while($newsItem = $this->db->fetch_array($query))
{
$news[$count] = $newsItem;
$count++;
}
return $news;
}
then with
getNews(14)
thats great Big Grin works now Big Grin cheers Big Grin