Dragon Blade Posted July 21, 2013 Posted July 21, 2013 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/****/public_html/****/drugfarm.php on line 114 Line 114 - while($r=mysql_fetch_array($q)) function grow() { global $ir,$h; echo" <h3><u>Drug Farm</u></h3>"; $q=mysql_query("SELECT * FROM items WHERE itmid = {$_GET['id']}"); while($r=mysql_fetch_array($q)) if($r['itmtype'] != 9) { echo"<hr width='50%'>Please read the error message.<hr width='50%'><h3>! ERROR</h3>*** .<br/><br/> <hr width='50%'><a href='drugfarm.php'>> Go Back</a><hr width='50%'><br />"; $h->endpage(); exit; } if(isset($_GET['id'])) { echo" <hr width='50%'>Please read the confirmation message.<hr width='50%'><h3>! CONFIRMATION</h3>You have planted the drug, you will have to wait for 3 days to receive them.<br/><br/> <hr width='50%'><a href='drugfarm.php'>> Go Back</a><hr width='50%'><br />"; mysql_query("INSERT INTO drug_farm VALUES('','{$ir['userid']}','{$_GET['id']}',3)"); $h->endpage(); exit; } else { echo"<hr width=75%>Here to grow some drugs? Well you came to the right place!<hr width=75%><br /><br />"; print "<hr width='75%'> <table width=75% cellspacing=1 class='table'> <tr style='background:gray'> <th>Drug Name</th> <th width=35%>Drug Description</th> <th>Buy Price</th> <th>Sell Price</th> <th>Links</th> </tr>"; $q=mysql_query("SELECT * FROM items WHERE itmtype=9"); while($r=mysql_fetch_array($q)) { print " <td><a href='iteminfo.php?ID={$r['itmid']}'>{$r['itmname']}</a></td> <td><div style='text-align: left; padding-left: 5px;'>{$r['itmdesc']}</div></td> <td>\${$r['itmbuyprice']}</td> <td>\${$r['itmsellprice']}</td> <td><a href='drugfarm.php?action=grow&id={$r['itmid']}'>Grow</a></td> </tr>"; } print "</table><hr width='75%'><br /><hr width='50%'><a href='drugfarm.php'>> Go Back</a><hr width='50%'><br />"; $h->endpage(); exit; } } If anyone can help me out, that would be great! Quote
Guest Posted July 21, 2013 Posted July 21, 2013 MySQL query returns a bool, i'm going to assume that item doesn't exist, therefore you need to check if it exists before doing the loop. Quote
Dragon Blade Posted July 21, 2013 Author Posted July 21, 2013 That only show's.... here [ATTACH=CONFIG]1073[/ATTACH] Quote
Guest Posted July 21, 2013 Posted July 21, 2013 That only show's.... here [ATTACH=CONFIG]1073[/ATTACH] What does that mean? Quote
Dragon Blade Posted July 21, 2013 Author Posted July 21, 2013 MySQL query returns a bool, i'm going to assume that item doesn't exist, therefore you need to check if it exists before doing the loop. Im not using the query to show the items? Im using another query Quote
Guest Posted July 21, 2013 Posted July 21, 2013 $q=mysql_query("SELECT * FROM items WHERE itmid = {$_GET['id']}"); while($r=mysql_fetch_array($q)) mysql_query returns a bool, so you're passing false into mysql_fetch_array http://uk1.php.net/mysql_query Quote
Alan Posted July 21, 2013 Posted July 21, 2013 The answers on this forum really do worry me at times. Has anybody actually looked at the documentation for mysql_query() ? From the PHP docs: For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.which means it is very easy to determine if your statement has worked or not:if (($rs = mysql_query($sql)) === false) { throw new Exception("..."); } else { /* do something sane with $rs */ /* ... */ mysql_free_result($rs); } The error provided even states the problem: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/****/public_html/****/drugfarm.php on line 114Although to be accurate, while a boolean has been passed instead of a resource, it is in fact False - which thankfully makes error checking a little easier.$q=mysql_query("SELECT * FROM items WHERE itmid = {$_GET['id']}"); is a pretty dreadful statement albeit in keeping with the rest of McCodes - perhaps a little defensive programming is in order? // ensure provided data looks like a number $id = array_key_exists('id', $_GET) && ctype_digit($_GET['id']) ? $_GET['id'] : 0; // construct sql statement $sql = "SELECT FROM `items` WHERE `itmid` = $id"; // execute the query $rs = mysql_query($sql); // error checking if ($rs === false) { throw new Exception('Database Error - Please contact staff'); exit; } // do something sane with $rs // ... // Optionally, release memory mysql_free_result($rs); Longer? Certainly Safer? Certainly Production quality? Not really, failing to pass the mysql link resource is unwise as is using the mysql extension itself Solves the problem? In this instance, from the information provided by the OP I'd say yes Quote
Guest Posted July 21, 2013 Posted July 21, 2013 The answers on this forum really do worry me at times. Has anybody actually looked at the documentation for mysql_query() ? From the PHP docs:which means it is very easy to determine if your statement has worked or not: if (($rs = mysql_query($sql)) === false) { throw new Exception("..."); } else { /* do something sane with $rs */ /* ... */ mysql_free_result($rs); } The error provided even states the problem:Although to be accurate, while a boolean has been passed instead of a resource, it is in fact False - which thankfully makes error checking a little easier. $q=mysql_query("SELECT * FROM items WHERE itmid = {$_GET['id']}"); is a pretty dreadful statement albeit in keeping with the rest of McCodes - perhaps a little defensive programming is in order? // ensure provided data looks like a number $id = array_key_exists('id', $_GET) && ctype_digit($_GET['id']) ? $_GET['id'] : 0; // construct sql statement $sql = "SELECT FROM `items` WHERE `itmid` = $id"; // execute the query $rs = mysql_query($sql); // error checking if ($rs === false) { throw new Exception('Database Error - Please contact staff'); exit; } // do something sane with $rs // ... // Optionally, release memory mysql_free_result($rs); Longer? Certainly Safer? Certainly Production quality? Not really, failing to pass the mysql link resource is unwise as is using the mysql extension itself Solves the problem? In this instance, from the information provided by the OP I'd say yes And why would the answers worry you? Quote
Djkanna Posted July 22, 2013 Posted July 22, 2013 And why would the answers worry you? MySQL query returns a bool, i'm going to assume that item doesn't exist, therefore you need to check if it exists before doing the loop. Because that is ( well partially - the _query returns a bool is partially right ) indeed wrong. The reason _query returned false is because the query failed, not because the item doesn't exist. Check that variables are set before trying to use them, it'll help...honestly. Quote
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.