Forum Moderators: coopster
i have vb forum and i write sql query to get random threads from the forum db and show it in my site home page (not in vb home page) after run the qury error make me qurizy .... i try too much to solve it but same error appear ..
this is the code :-
==========
$threadchars = "30";
$html = "<table>";
$getstats_threads = ("
SELECT thread.threadid, thread.title, thread.lastpost, thread.forumid, thread.replycount, thread.lastposter, thread.dateline, thread.visible, user.username, user.userid
FROM thread AS thread
LEFT JOIN user AS user ON (user.username = thread.lastposter)
ORDER BY RAND() LIMIT 5");
if(mysql_num_rows($getstats_threads)==0) //this is line 36
{
echo 'Sorry no record';
}
else
{
while ($getstats_thread = $fetch_array($getstats_threads))
{
$getstats_thread[title] = unhtmlspecialchars($getstats_thread[title]);
if (strlen($getstats_thread[title]) > $threadchars)
{
$getstats_thread[titletrimmed] = substr($getstats_thread[title], 0, strrpos(substr($getstats_thread[title], 0, $threadchars), ' ')) . '...';
}
else
{
$getstats_thread[titletrimmed] = $getstats_thread[title];
}
('forumhome_stats_thread') . '";');
$html.="<tr><td><b><a href='showthread.php?$session[sessionurl]goto=newpost&t=$getstats_thread[threadid]'>$getstats_thread[titletrimmed]</a></b></td>";
$html.="<td><b><a href='member.php?$session[sessionurl]u=$getstats_thread[userid]' target='_blank'>$getstats_thread[lastposter]</a></b></td>";
$html.="<td><b>$getstats_thread[views]</b></td>";
$html.="<td><b>$getstats_thread[replycount]</b>";
}
}
$html .= "</td></tr></table></td></tr></table></td></table>";
echo $html
============
and this is the error:
------------
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/sitename/public_html/randm.php on line 36
Sorry no record
Table 'site_db.sessions' doesn't exist
-------------
any help will appreciated
I know PHP but use it with MSSQL rather than mySQL so i'm sorry if this answer is total rubbish!
It looks to me like you haven't executed your query which is why you are getting the error. The num_rows function needs to be run on a result set not on a query.
So,
$sql = "SELECT thread.threadid, thread.title, thread.lastpost, thread.forumid, thread.replycount, thread.lastposter, thread.dateline, thread.visible, user.username, user.userid
FROM thread AS thread
LEFT JOIN user AS user ON (user.username = thread.lastposter)
ORDER BY RAND() LIMIT 5"
$getstats_threads = mysql_query($sql);
if(mysql_num_rows($getstats_threads)==0) //this is line 36
etc etc
Hope this helps
$getstats_threads = ("
SELECT thread.threadid, thread.title, thread.lastpost, thread.forumid, thread.replycount, thread.lastposter, thread.dateline, thread.visible, user.username, user.userid
FROM thread AS thread
LEFT JOIN user AS user ON (user.username = thread.lastposter)
ORDER BY RAND() LIMIT 5");
to
$getstats_threads = mysql_query("
SELECT thread.threadid, thread.title, thread.lastpost, thread.forumid, thread.replycount, thread.lastposter, thread.dateline, thread.visible, user.username, user.userid
FROM thread AS thread
LEFT JOIN user AS user ON (user.username = thread.lastposter)
ORDER BY RAND() LIMIT 5");
dc
i did what you say and i think itis correct because i not get error in line 36 i get error in line 44 ....
this is the error :
---------------------
Fatal error: Call to undefined function: () in /home/sitename/public_html/rand.php on line 44
Table 'sitename_db.sessions' doesn't exist
-------------------
and line 44 shown here after modify the code :
==================
...
.
.
$sql = ("
SELECT thread.threadid, thread.title, thread.lastpost, thread.forumid, thread.replycount, thread.lastposter, thread.dateline, thread.visible, user.username, user.userid
FROM thread AS thread
LEFT JOIN user AS user ON (user.username = thread.lastposter)
ORDER BY RAND() LIMIT 5");
$getstats_threads = mysql_query($sql);
if(mysql_num_rows($getstats_threads)==0)
{
echo 'Sorry no record';
}
else
{
while ($getstats_thread = $fetch_array($getstats_threads)) // <<<<< this is line 44
{
$getstats_thread[title] = unhtmlspecialchars($getstats_thread[title]);
if (strlen($getstats_thread[title]) > $threadchars)
{
$getstats_thread[titletrimmed] = substr($getstats_thread[title], 0, strrpos(substr($getstats_thread[title], 0, $threadchars), ' ')) . '...';
}
else
{
$getstats_thread[titletrimmed] = $getstats_thread[title];
}
.
.
.
.
===============