oskom Posted December 5, 2007 Share Posted December 5, 2007 Hello all, I've been surfing forum threads on this, and there are plenty, with no satisfactory results. Here's the deal... I've got quite a few instances of this very common query code on my site: //example 1 $result = mysql_query("SELECT * FROM table WHERE ID = ".$_REQUEST['ID'].""); $row = mysql_fetch_array($result); //LINE OF CODE IN ERROR //example 2 $result = mysql_query("SELECT * FROM table WHERE ID = ".$_REQUEST['ID'].""); while($row = mysql_fetch_array($result)) { //LINE OF CODE IN ERROR //LOOP DATA } The PHP error log will frequently give a warning to this effect:"PHP Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /usr/local/ftp/path/to/file.php on line 15". The important thing to know is that I ALWAYS get the desired result from the database. I've added "or die(mysql_error())" to the end of the queries with no error being displayed and echoing the $result will always give me the resource id number. If I'm getting the data, why would there be a warning? Most of the diagnoses of this error in these forum threads I've been reading keep mentioning bad queries or database connections. Apparently, neither of those things are the problem if I'm actually getting results. Enlightenment? Quote Link to comment Share on other sites More sharing options...
oskom Posted December 5, 2007 Author Share Posted December 5, 2007 hello? Quote Link to comment Share on other sites More sharing options...
revraz Posted December 5, 2007 Share Posted December 5, 2007 Means something was wrong with your query. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 5, 2007 Share Posted December 5, 2007 Replace your examples with: <?php //example 1 $q = "SELECT * FROM table WHERE ID = ".$_REQUEST['ID']; $result = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); $row = mysql_fetch_array($result); //LINE OF CODE IN ERROR //example 2 $q = "SELECT * FROM table WHERE ID = ".$_REQUEST['ID']; $result = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); while($row = mysql_fetch_array($result)) { //LINE OF CODE IN ERROR //LOOP DATA }?> This should tell you what the problem is. Ken Quote Link to comment Share on other sites More sharing options...
oskom Posted December 5, 2007 Author Share Posted December 5, 2007 I can only reassert: I've already done all of those things already!!!! Rewriting the code(as I have just done to test it) like this... $result = mysql_query("SELECT * FROM table WHERE ID = ".$_REQUEST['ID']."") or die("Problem with the query: $q<br>" . mysql_error()); $row = mysql_fetch_array($result); //LINE OF CODE IN ERROR ...will display no errors on the page itself. I get the data I wanted just fine. Whether or not the "die()" statement exists after the query seems to be irrelevant, which says to me that the query is fine. The more telling thing is that I'm actually GETTING DATA! MySQL likes my query enough to give me the data but PHP gets all persnickety about something. Quote Link to comment 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.