JawdS Posted November 17, 2012 Share Posted November 17, 2012 (edited) HI guys, Everytime I run my php code I get this error message: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a3651864/public_html/footwear.php on line 96 This is my code: $query = mysql_query("SELECT * FROM Products WHERE cat='Skate' AND subcat='Footwear' LIMIT $start, $limit_img "); //end of query $limit_img = 24; $start = 0; $cols = 3; $cols_td = 0; //end of table variables $images = $row['photo']; $product = $row['product']; $price = $row['price']; //end of row variables echo "<table width='95%'><tr>"; //end of table while ($row = mysql_fetch_array($query)){ echo "<td><div align='center'>$row[product]</div></td>"; $cols_td = $col_td + 1; if($cols_td == $cols){ echo "</tr><tr>"; $cols_td = 0; } } //end of while statement echo "</tr></table>"; Thanks In advance! Edited November 17, 2012 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/270824-warning-mysql_fetch_array-need-help/ Share on other sites More sharing options...
Zane Posted November 17, 2012 Share Posted November 17, 2012 That PHP error message most always means that your query is invalid. You should put this at the end of your query statement so we can see the SQL error $query = mysql_query("SELECT * FROM Products WHERE cat='Skate' AND subcat='Footwear' LIMIT $start, $limit_img ") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/270824-warning-mysql_fetch_array-need-help/#findComment-1393167 Share on other sites More sharing options...
Barand Posted November 17, 2012 Share Posted November 17, 2012 Also echoing the query to see if it looks right can often make it obvious what is wrong. echo $query; Quote Link to comment https://forums.phpfreaks.com/topic/270824-warning-mysql_fetch_array-need-help/#findComment-1393173 Share on other sites More sharing options...
Andy123 Posted November 17, 2012 Share Posted November 17, 2012 As indicated above, it's caused by mysql_query() not returning a resource. It returns FALSE on failure. mysql_fetch_array() expects a resource, but in your case, $query is not a resource. Fixing your query will obviously do the trick, but I would also validate that the query was successful just in case things break in the future. $query = mysql_query("SELECT * FROM Products WHERE cat='Skate' AND subcat='Footwear' LIMIT $start, $limit_img"); if ($query) { // Query successful if (mysql_num_rows($query) > 0) { // Successful and at least one row returned } else { // Successful but no rows returned } } In this way, you can present user friendly error messages along with logging for debugging purposes. Quote Link to comment https://forums.phpfreaks.com/topic/270824-warning-mysql_fetch_array-need-help/#findComment-1393228 Share on other sites More sharing options...
swisse Posted November 17, 2012 Share Posted November 17, 2012 Hi JawdS, $row[product] lacks single quotation marks, thus it should read $row['product']. Quote Link to comment https://forums.phpfreaks.com/topic/270824-warning-mysql_fetch_array-need-help/#findComment-1393296 Share on other sites More sharing options...
Pikachu2000 Posted November 17, 2012 Share Posted November 17, 2012 (edited) Technically, when an associative array element is in a quoted string, the index doesn't need to be quoted. If the index is quoted, complex notation needs to be used, e.g., $str = "quoted string with {$array['index']} in it"; Edited November 17, 2012 by Pikachu2000 Quote Link to comment https://forums.phpfreaks.com/topic/270824-warning-mysql_fetch_array-need-help/#findComment-1393298 Share on other sites More sharing options...
Barand Posted November 18, 2012 Share Posted November 18, 2012 I'd look at the order in which these statements occur $query = mysql_query("SELECT * FROM Products WHERE cat='Skate' AND subcat='Footwear' LIMIT $start, $limit_img "); //end of query $limit_img = 24; $start = 0; Quote Link to comment https://forums.phpfreaks.com/topic/270824-warning-mysql_fetch_array-need-help/#findComment-1393306 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.