MyBB Community Forums

Full Version: My own little mod...help!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to get the 5 latest news thread subjects to display on the homepage of my website (not the forum). Only problem is I keep getting an error: "Resource id #2". Here is the code:

<?php

	$host = "xxxxxxxxxx";
	$user = "xxxxxxxxxx";
	$pass = "xxxxxxxxxx";
	$dbs = "xxxxxxxxxx";
	$query = "SELECT `subject` FROM `mybb_threads` WHERE `fid` = '5' ORDER BY `tid` DESC";
	
if ($connection = mysql_connect($host,$user,$pass))
{

if ($db = mysql_select_db($dbs,$connection))
{
$result = mysql_query($query)

            for($i = 0;$i < 5;$i++)
            {
             echo "$result<br />";
            }
            
}
}
else{}
?>

Any help with this problem would be really great!

-DrPoodle
you forget to fetch result from query.
read source of mybb abit speacially /inc/db_mysql.php
btw your solution is here
mysql_fetch_array
$result = mysql_fetch_array(mysql_query($query),MYSQL_ASSOC);
Try:
<?php
$host = "xxxxxxxxxx";
$user = "xxxxxxxxxx";
$pass = "xxxxxxxxxx";
$dbs = "xxxxxxxxxx";

$connection = mysql_connect($host,$user,$pass);
$db = mysql_select_db($dbs,$connection);

$query = "SELECT `subject` FROM `mybb_threads` WHERE `fid` = '5' AND `visible` = '1' ORDER BY `tid` DESC LIMIT 0,5";
    
if ($query)
{
while($result = mysql_fetch_array($query))
    {
    echo $result['subject']."<br />";
    }

}
else{}
?>
ok, thanks for all the help so far. Michael83's method worked...sort of. it just displays 5 of the SAME subject, instead of the 5 latest thread subjects. here is the Michael83's code that i modified to work:

<?php
$host = "xxxxxxxx";
$user = "xxxxxxxx";
$pass = "xxxxxxxx";
$dbs = "xxxxxxxx";

$connection = mysql_connect($host,$user,$pass);
$db = mysql_select_db($dbs,$connection);

$query = "SELECT `subject` FROM `mybb_threads` WHERE `fid` = 5 ORDER BY `tid` DESC";

if ($query)
{
$result = mysql_fetch_array(mysql_query($query),MYSQL_ASSOC);
for($i = 0;$i < 5;$i++)
    {
    echo $result['subject']."<br />";
    }

}
else{}
?>
micheals code is right
just modify:
limite 5
Ok, I get this response:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/html/news.php on line 16

Line 16 reads:
while($result = mysql_fetch_array($query))
It means your query is probably wrong. Try running it through the mysql tab on phpMyAdmin, that should tell you where the problem is.
You know you can use $db->query instead of having to make the connection yourself right? That is if you are using MyBB files Smile.
Nope, it works perfectly in phpMyAdmin! displays the list of the 5 most recent threads! Here is the full code again so that other people might be able to solve it!

<?php
$host = "localhost";
$user = "xxxx";
$pass = "xxxx";
$dbs = "xxxx";

$connection = mysql_connect($host,$user,$pass);
$db = mysql_select_db($dbs,$connection);

$query = "SELECT `subject` FROM `zzzmyforum_threads` WHERE `fid` = '5' AND `visible` = '1' ORDER BY `tid` DESC LIMIT 0,5";

if ($query)
{
while($result = mysql_fetch_array($query))
    {
    echo $result['subject']."<br />";
    }

}
else{}
?>
Sorry for that. The line with the query has to be:
$query = mysql_query("SELECT `subject` FROM `zzzmyforum_threads` WHERE `fid` = '5' AND `visible` = '1' ORDER BY `tid` DESC LIMIT 0,5");
Yay! It works! Thanks to everyone who helped! The finished result can be seen here: http://www.drpoodle.com (look at the news bit)