jaredtime Posted July 23, 2007 Share Posted July 23, 2007 So this is my code, I get this error when I run it. I am fairly new to PHP, so I don't know how to fix. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/14825/domains/theohroom.com/html/config.php on line 27 Line 27 in my code is this one if(mysql_num_rows($result)) <?php $phpld = "2.0"; // Database Information // MySQL Host (usually localhost) $mysql_host = ""; // MySQL Database Name $database_name = ""; // MySQL User Name $database_user = ""; // MySQL User Password $user_password = "; $connection = @mysql_connect ($mysql_host,$database_user, $user_password) or die ("Cannot make the connection"); //connect to database $db = @mysql_select_db ($database_name,$connection) or die ("Cannot connect to database."); $now = time(); if ($now - $_SESSION['cache_time'] > $cachetime || empty($_SESSION['cache_time']) || $_SESSION['adminloggedin'] == 1){ $_SESSION['cache_time'] = time(); // Pulls all the Site Config data from the database $query = "select * from siteconfig"; $result = mysql_query($query); if(mysql_num_rows($result)) { while($row = mysql_fetch_array($result)) { $base_url = $row['baseurl']; $_SESSION['base_url'] = $base_url; $rewrite = $row['sefriendly']; $_SESSION['rewrite'] = $rewrite; $sitename = $row['sitename']; $_SESSION['sitename'] = $sitename; $sitekeywords = $row['keywords']; $_SESSION['sitekeywords'] = $sitekeywords; $sitedescription = $row['description']; $_SESSION['sitedescription'] = $sitedescription; $frameout = $row['frameout']; $_SESSION['frameout'] = $frameout; $submissions = $row['submissions']; $_SESSION['submissions'] = $submissions; $trackclicks = $row['trackclicks']; $_SESSION['trackclicks'] = $trackclicks; $logusers = $row['logusers']; $_SESSION['logusers'] = $logusers; $reports = $row['reports']; $_SESSION['reports'] = $reports; $maxpersite = $row['maxpersite']; $_SESSION['maxpersite'] = $maxpersite; $max_results = $row['maxresults']; $_SESSION['max_results'] = $max_results; $bannedmessage = $row['bannedmessage']; $_SESSION['bannedmessage'] = $bannedmessage; $delay = $row['delay']; $_SESSION['delay'] = $delay; $sitestatus = $row['sitestatus']; $_SESSION['sitestatus'] = $sitestatus; $offlinemessage = stripslashes(nl2br($row['offlinemessage'])); $_SESSION['offlinemessage'] = $offlinemessage; $prune = $row['prune']; $_SESSION['prune'] = $prune; $maxtextplugs = $row['maxtextplugs']; $_SESSION['maxtextplugs'] = $maxtextplugs; $maximageplugs = $row['maximageplugs']; $_SESSION['maximageplugs'] = $maximageplugs; $linktrading = $row['linktrading']; $_SESSION['linktrading'] = $linktrading; $maxtoplinks = $row['maxtoplinks']; $_SESSION['maxtoplinks'] = $maxtoplinks; $sorttoplinks = $row['sorttoplinks']; $_SESSION['sorttoplinks'] = $sorttoplinks; $sortlinks = $row['sortlinks']; $_SESSION['sortlinks'] = $row['sortlinks']; $realurl = $row['realurl']; $_SESSION['realurl'] = $realurl; $default_user = stripslashes($row['defaultusername']); $_SESSION['defaultuser'] = $default_user; $member_comment = $row['membercomment']; $_SESSION['membercomment'] = $member_comment; $member_submit = $row['membersubmit']; $_SESSION['membersubmit'] = $member_submit; $email_confirmation = $row['emailconfirmation']; $_SESSION['emailconfirmation'] = $email_confirmation; $require_thumb = $row['requirethumb']; $_SESSION['requirethumb'] = $require_thumb; $auto_chmod = $row['autochmod']; $_SESSION['autochmod'] = $auto_chmod; $max_thumb_height = $row['maxthumbheight']; $_SESSION['maxthumbheight'] = $max_thumb_height; $max_thumb_width = $row['maxthumbwidth']; $_SESSION['maxthumbwidth'] = $max_thumb_width; $default_image = $row['defaultimage']; $_SESSION['defaultimage'] = $default_image; $max_publish = $row['maxpublish']; $_SESSION['maxpublish'] = $max_publish; $publishtime = $row['publishtime']; $_SESSION['publishtime'] = $publishtime; $contact_email = $row['contactemail']; $_SESSION['contactemail'] = $contact_email; $cachetime = $row['cachetime']; $_SESSION['cachetime'] = $cachetime; $member_login = $row['memberlogin']; $_SESSION['memberlogin'] = $member_login; $max_comments = $row['maxcomments']; $_SESSION['maxcomments'] = $max_comments; $requirethumb = $row['requirethumb']; $_SESSION['requirethumb'] = $requirethumb; $skim = $row['skim']; $_SESSION['skim'] = $skim; $skimurl = $row['skimurl']; $_SESSION['skimurl'] = $skimurl; $template_directory = $row['templatedirectory']; $_SESSION['templatedirectory'] = $template_directory; } } else{ die; } }else{ $base_url = $_SESSION['base_url']; $rewrite = $_SESSION['rewrite']; $sitename = $_SESSION['sitename']; $sitekeywords = $_SESSION['sitekeywords']; $sitedescription = $_SESSION['sitedescription']; $frameout = $_SESSION['frameout']; $submissions = $_SESSION['submissions']; $trackclicks = $_SESSION['trackclicks']; $logusers = $_SESSION['logusers']; $reports = $_SESSION['reports']; $maxpersite = $_SESSION['maxpersite']; $max_results = $_SESSION['max_results']; $bannedmessage = $_SESSION['bannedmessage']; $delay = $_SESSION['delay']; $sitestatus = $_SESSION['sitestatus']; $offlinemessage = $_SESSION['offlinemessage']; $prune = $_SESSION['prune']; $maxtextplugs = $_SESSION['maxtextplugs']; $maximageplugs = $_SESSION['maximageplugs']; $linktrading = $_SESSION['linktrading']; $maxtoplinks = $_SESSION['maxtoplinks']; $sorttoplinks = $_SESSION['sorttoplinks']; $sortlinks = $_SESSION['sortlinks']; $realurl = $_SESSION['realurl']; $default_user = $_SESSION['defaultusername']; $member_comment = $_SESSION['membercomment']; $member_submit = $_SESSION['membersubmit']; $email_confirmation = $_SESSION['emailconfirmation']; $require_thumb = $_SESSION['requirethumb']; $auto_chmod = $_SESSION['autochmod']; $max_thumb_height = $_SESSION['maxthumbheight']; $max_thumb_width = $_SESSION['maxthumbwidth']; $default_image = $_SESSION['defaultimage']; $max_publish = $_SESSION['maxpublish']; $publishtime = $_SESSION['publishtime']; $contact_email = $_SESSION['contactemail']; $cachetime = $_SESSION['cachetime']; $member_login = $_SESSION['memberlogin']; $max_comments = $_SESSION['maxcomments']; $requirethumb = $_SESSION['requirethumb']; $skim = $_SESSION['skim']; $skimurl = $_SESSION['skimurl']; $template_directory = $_SESSION['templatedirectory']; } return; ?> If anyone can help me out that would be awesome. Thanks a lot! Link to comment https://forums.phpfreaks.com/topic/61320-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resource/ Share on other sites More sharing options...
kael.shipman Posted July 23, 2007 Share Posted July 23, 2007 Easy fix! don't test for mysql_fetch_row() as being true or false; test $result itself, then test the number of rows returned: <?php $result = @mysql_query("select something from somewhere where col = 'val'"); //Return result if successful, false if errors occurred. if (!$result) echo 'MYSQL ERROR: '.mysql_error(); //if there was an error, spit it out elseif (mysql_num_rows($result)) { //Otherwise, if there aren't any rows returned, this gives you 0, which evaluates to false and doesn't run the code block while ($row = @mysql_fetch_row($result)) { //stuff } } } ?> Link to comment https://forums.phpfreaks.com/topic/61320-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-305139 Share on other sites More sharing options...
ViN86 Posted July 23, 2007 Share Posted July 23, 2007 could also do: <?php if ($result = mysql_query("sql statement")){ // code where query was successfully completed } else { // code to output error } gl Link to comment https://forums.phpfreaks.com/topic/61320-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-305140 Share on other sites More sharing options...
JayBachatero Posted July 23, 2007 Share Posted July 23, 2007 Try fixing the parse error first. // MySQL User Password $user_password = "; Link to comment https://forums.phpfreaks.com/topic/61320-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-305148 Share on other sites More sharing options...
DeadEvil Posted July 23, 2007 Share Posted July 23, 2007 change this part.. if(mysql_num_rows($result)) { .... .. with this one to make sure your quiry is not empty $num= mysql_num_rows($result); #do this if theres a result... if($num > 0) { #statement..... } Both those pieces of code do exactly the same thing, the first is just shorter. Link to comment https://forums.phpfreaks.com/topic/61320-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-305283 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.