jeppers Posted August 22, 2008 Share Posted August 22, 2008 i am working on a forum script and i am trying to pull some info from a database. but i keep on getting this message "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\test\discussion\viewforum.php on line 32" i have looked back through my script and can't seem to see any errors but maybe i am missing something silly. hear is my code please have a look as its completely baffled me. $forumsql = "SELECT * FROM forums WHERE id = " . $validforum . ";"; $forumresult = mysql_query($forumsql); $forumrow = mysql_fetch_assoc($forumresult); echo "<h2>" . $forumrow['name'] . "</h2>"; echo "<a href='index.php'>" . $config_forumsname . " forums</><br /><br />"; //add a link that allows you to add a new topic to this form echo "[<a href='newtopic.php?id= " . $validforum . "'>New Topic</a>]"; echo "<br /><br />"; $topicsql = "SELECT MAX (message.date) AS maxdate, topics.id AS topicid, topics.*, users.* FROM messages, topics, users WHERE messages.topic_id = topic.id AND topics.user_id = user.id AND topics.forum_id ORDER BY maxdate DESC;"; $topicresult = mysql_query($topicsql); $topicnumrows = mysql_num_rows($topicresult); // line 32 this is where the error said it was happening.... if ($topicnumrows == 0) { echo "<table width='300px'><tr><td>No Topics!!</td></tr></table>"; hopefully you can see where i have went wrong thanks Quote Link to comment https://forums.phpfreaks.com/topic/120857-pulling-infomation-from-a-database-simple-error/ Share on other sites More sharing options...
JonnoTheDev Posted August 22, 2008 Share Posted August 22, 2008 check the query is correct. You should not continue to process the script if the query produces an error which is what you are doing. You need to handle errors. A simple method is: if(!$result = mysql_query("SELECT ...")) { die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/120857-pulling-infomation-from-a-database-simple-error/#findComment-622976 Share on other sites More sharing options...
jeppers Posted August 22, 2008 Author Share Posted August 22, 2008 not sure how to inter great that pice of script with in my current script if(!$result = mysql_query("SELECT ...")) { die(mysql_error()); } any chance you could give me an example on how you would do it thanks Quote Link to comment https://forums.phpfreaks.com/topic/120857-pulling-infomation-from-a-database-simple-error/#findComment-622986 Share on other sites More sharing options...
nitation Posted August 22, 2008 Share Posted August 22, 2008 @ jeppers, Try this; $topicsql = mysql_query("SELECT MAX (message.date) AS maxdate, topics.id AS topicid, topics.*, users.* FROM messages, topics, users WHERE messages.topic_id = topic.id AND topics.user_id = user.id AND topics.forum_id ORDER BY maxdate DESC") or die (mysql_error()); note the "or die mysql_error()); should tell where your error is coming from. I also observe something in your query, why are you terminating immediately after maxdate DESC. Regards Quote Link to comment https://forums.phpfreaks.com/topic/120857-pulling-infomation-from-a-database-simple-error/#findComment-622990 Share on other sites More sharing options...
jeppers Posted August 22, 2008 Author Share Posted August 22, 2008 well hear is what has been returned FUNCTION forum.MAX does not exist. not sure what to do erm Quote Link to comment https://forums.phpfreaks.com/topic/120857-pulling-infomation-from-a-database-simple-error/#findComment-622995 Share on other sites More sharing options...
JonnoTheDev Posted August 22, 2008 Share Posted August 22, 2008 If the query is incorrect the script will terminate. If OK the results of the query are in $forumrow. Apply to the rest of your logic. if(!$forumresult = mysql_query($forumsql)) { die(mysql_error()); exit(); } $forumrow = mysql_fetch_assoc($forumresult); Quote Link to comment https://forums.phpfreaks.com/topic/120857-pulling-infomation-from-a-database-simple-error/#findComment-622999 Share on other sites More sharing options...
jeppers Posted August 22, 2008 Author Share Posted August 22, 2008 not sure what the problem it but the error which is poping up is still FUNCTION forum.MAX does not exist so the first query is correct but the error is within the second query. all around the MAX function have you any ideas. Quote Link to comment https://forums.phpfreaks.com/topic/120857-pulling-infomation-from-a-database-simple-error/#findComment-623008 Share on other sites More sharing options...
jeppers Posted August 22, 2008 Author Share Posted August 22, 2008 $topicsql = "SELECT MAX(messages.date) AS maxdate, topics.id AS topicid, topics.*, users.* FROM messages, topics, users WHERE messages.topic_id = topics.id AND topics.user_id = users.id AND topics.forum_id ORDER BY maxdate DESC;"; if (!$topicresult = mysql_query($topicsql)) { die(mysql_error()); exit(); well i found a few errors so not to bad but i have hit another error report which i have not see before. Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause i no what it means but where in the code is the issue can you see it Quote Link to comment https://forums.phpfreaks.com/topic/120857-pulling-infomation-from-a-database-simple-error/#findComment-623019 Share on other sites More sharing options...
JonnoTheDev Posted August 22, 2008 Share Posted August 22, 2008 if you use the mysql MAX() function you have to include a GROUP BY statement. i.e. SELECT MAX(fieldName) FROM table GROUP BY id ORDER BY id DESC Quote Link to comment https://forums.phpfreaks.com/topic/120857-pulling-infomation-from-a-database-simple-error/#findComment-623021 Share on other sites More sharing options...
jeppers Posted August 22, 2008 Author Share Posted August 22, 2008 thanks very much all done and thanks thanks for the error location tools. hope i will be able to help you soon Quote Link to comment https://forums.phpfreaks.com/topic/120857-pulling-infomation-from-a-database-simple-error/#findComment-623028 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.