jakerow Posted September 20, 2011 Share Posted September 20, 2011 Thanks for reading! I'm working on a site and it won't display my records from the database. I have a little filter system that did work, but now it doesn't. I get the following error message: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/vhosts/60608.glrdmd.eu/httpdocs/jakesite/select3.php on line 26 Relevant code to the error is: <?php include("connection.php"); if(isset($_POST['Graphic']) && $_POST['Graphic'] == 'yes') { $result1 = "graphic"; } if(isset($_POST['Interactive']) && $_POST['Interactive'] == 'yes') { $result2 = "interactive"; } if(isset($_POST['Websites']) && $_POST['Websites'] == 'yes') { $result3 = "websites"; } $order = $_POST['group1']; $sql = ("SELECT * FROM 'jake_portfolio' WHERE CAT = '$result1' OR CAT = '$result2' OR CAT = '$result3' ORDER BY '$order' LIMIT 0, 4"); $result = mysql_query($sql); $checked = "checked"; $row = mysql_fetch_array($result); ?> Does anyone see what I am doing wrong here? Quote Link to comment https://forums.phpfreaks.com/topic/247521-supplied-argument-is-not-a-valid-mysql-result-resource/ Share on other sites More sharing options...
Pikachu2000 Posted September 20, 2011 Share Posted September 20, 2011 The query is failing because table names do not get enclosed in quotes. Beyond that if there are other errors, echo mysql_error and see what they are. Quote Link to comment https://forums.phpfreaks.com/topic/247521-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-1271058 Share on other sites More sharing options...
jakerow Posted September 20, 2011 Author Share Posted September 20, 2011 I have removed those quotes, I didn't have them there in the first place but I read somewhere that it might fix the problem? Anyway I put this in: $row = if(!mysql_fetch_array($result)){ echo mysql_error()}; And now I get the following error: Parse error: syntax error, unexpected T_IF in /var/www/vhosts/60608.glrdmd.eu/httpdocs/jakesite/select3.php on line 26 Quote Link to comment https://forums.phpfreaks.com/topic/247521-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-1271076 Share on other sites More sharing options...
Pikachu2000 Posted September 20, 2011 Share Posted September 20, 2011 This instead: $result = mysql_query($sql) or die( "<br>Query string: $sql<br>Produced error: " . mysql_error() ); This would be for debugging only, BTW. On a live site, you would never want to echo the actual query string or MySQL error to the user. Quote Link to comment https://forums.phpfreaks.com/topic/247521-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-1271080 Share on other sites More sharing options...
jakerow Posted September 20, 2011 Author Share Posted September 20, 2011 Your suggested debugging solution provided me with this: Query string: SELECT * FROM jake_portfolio WHERE CAT = OR CAT = OR CAT = ORDER BY LIMIT 0, 4 Produced error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OR CAT = OR CAT = ORDER BY LIMIT 0, 4' at line 1 So what is wrong with with my coding in that part? I have removed the ''s Quote Link to comment https://forums.phpfreaks.com/topic/247521-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-1271091 Share on other sites More sharing options...
Pikachu2000 Posted September 20, 2011 Share Posted September 20, 2011 All of your variables are empty. You'd need to post the form markup and any other relevant code so we can see what's up with it and offer suggestions . . . Quote Link to comment https://forums.phpfreaks.com/topic/247521-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-1271094 Share on other sites More sharing options...
jakerow Posted September 20, 2011 Author Share Posted September 20, 2011 The form is as follows: <div id="filtermenu"> <form action="select3.php" method="post"> Search: <p> <input name="Graphic" type="checkbox" value="yes" <?PHP if($result1 == 'graphic'){print $checked ; }?>/> Graphic<br /> <input name="Interactive" type="checkbox" value="yes"<?PHP if($result2 == 'interactive'){print $checked ; }?>/> Interactive<br /> <input name="Websites" type="checkbox" value="yes" <?PHP if($result3 == 'websites'){print $checked ; }?>/> Websites</p> Sort by: <p> <input name="group1" type="radio" value="DATE DESC" <?PHP if($order == 'DATE DESC'){print $checked ; } elseif($order != 'DATE ASC' or 'VIEWS'){print $checked ;}?>/> Newest<br /> <input name="group1" type="radio" value="DATE ASC" <?PHP if($order== 'DATE ASC'){print $checked ; }?>/> Oldest<br /> <input name="group1" type="radio" value="VIEWS" <?PHP if($order == 'VIEWS'){print $checked ; }?>/> Most viewed</p> <input name="Search!" type="submit" value="Filter" id="button"/> </form> thanks for taking your time for me by the way. Quote Link to comment https://forums.phpfreaks.com/topic/247521-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-1271096 Share on other sites More sharing options...
Pikachu2000 Posted September 20, 2011 Share Posted September 20, 2011 One other thought. Is this problem occurring only before the form has been submitted, or after the form has been submitted also? Quote Link to comment https://forums.phpfreaks.com/topic/247521-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-1271103 Share on other sites More sharing options...
jakerow Posted September 20, 2011 Author Share Posted September 20, 2011 I use the form to remember which boxes are checked etc. but it doesn't give me anything from the database. It happens before and after though. Quote Link to comment https://forums.phpfreaks.com/topic/247521-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-1271106 Share on other sites More sharing options...
Pikachu2000 Posted September 20, 2011 Share Posted September 20, 2011 To stop the issue before submission, you need to make sure the form has been submitted before allowing the query to run. To do that, enclose everything after the include() in a conditional that checks whether the form has been sent. After that, see what errors are returned when the form is submitted, and post them along with the revised code. <?php include("connection.php"); if( strtolower($_SERVER['REQUEST_METHOD']) === 'post' ) { // rest of code } Quote Link to comment https://forums.phpfreaks.com/topic/247521-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-1271127 Share on other sites More sharing options...
jakerow Posted September 21, 2011 Author Share Posted September 21, 2011 Hey Pikachu, allthough it didn't complete the problem it lead to some insights and it did finally work. Thanks a lot for your help! Quote Link to comment https://forums.phpfreaks.com/topic/247521-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-1271339 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.