esscher Posted February 6, 2008 Share Posted February 6, 2008 I have a query that does not always bring up a result, and thus echoes this: Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\wamp\www\phytobase\p2\try3.php on line 35 Is it bad coding to have this happen, even if I turn off "display errors" in php.ini? Scott Quote Link to comment https://forums.phpfreaks.com/topic/89758-solved-is-this-bad-coding-practice/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 6, 2008 Share Posted February 6, 2008 The error means your query failed for some reason. This not the same as a query that worked but matched zero rows. The error is probably due to a syntax error in the query if you are building it dynamically using form data but the data was not as expected. You always need to check if the mysql_query() returned a false value (query failed) before attempting to access the result resource. Quote Link to comment https://forums.phpfreaks.com/topic/89758-solved-is-this-bad-coding-practice/#findComment-459977 Share on other sites More sharing options...
trq Posted February 6, 2008 Share Posted February 6, 2008 You always need to check if the mysql_query() returned a false value (query failed) before attempting to access the result resource. Just alaberating on this a bit. Its something newcomers do all the time, not checking returned values. I always see... <?php $result = mysql_query("SELECT * FROM foo"); while ($row = mysql_fetch_assoc($result)) { // do something with data. } ?> This is terrible. At minimum, to get the same job done use something like... <?php $sql = "SELECT * FROM foo"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { // do something with data. } } else { // no records returned. } } else { // query failed, handle error // you can even use the $sql variable here to debug your query // you also have access to the mysql_error() function for debugging. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89758-solved-is-this-bad-coding-practice/#findComment-459985 Share on other sites More sharing options...
Psycho Posted February 6, 2008 Share Posted February 6, 2008 @thorpe, Nice to see you associating the query with a variable. It is a pet peeve of mine to see people put the actual text of the query within the mysql_query() function and then post questions as to why their query is not working. If they were to assign the query to a variable they could simply write it to the screen to see what the error is. Quote Link to comment https://forums.phpfreaks.com/topic/89758-solved-is-this-bad-coding-practice/#findComment-459997 Share on other sites More sharing options...
esscher Posted February 6, 2008 Author Share Posted February 6, 2008 Thanks very much.. thats awesome help. I'll see if it helps fix my problem.. Quote Link to comment https://forums.phpfreaks.com/topic/89758-solved-is-this-bad-coding-practice/#findComment-460023 Share on other sites More sharing options...
esscher Posted February 6, 2008 Author Share Posted February 6, 2008 Yep, worked like a charm. Quote Link to comment https://forums.phpfreaks.com/topic/89758-solved-is-this-bad-coding-practice/#findComment-460067 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.