Craig Hopson Posted July 15, 2012 Share Posted July 15, 2012 hi guys i have this error mysql_fetch_array() expects parameter 1 to be resource, boolean given in on line : comes from this code<?php$result = mysql_query("SELECT * FROM files ORDER BY RAND() LIMIT 10");while ($row = mysql_fetch_array($result)) { $result2 = mysql_query("SELECT * FROM members WHERE id = $row[subject] "); while ($row2 = mysql_fetch_array($result2)) { if ($row2['public'] == "1") { $img = 'uploads/' . $row['subject'] . '/' . $row['filename']; echo '<img src="' . $img . '" style="border:0px;"/>'; } }}?> just started for giving error i dont know why? Link to comment Share on other sites More sharing options...
niche Posted July 15, 2012 Share Posted July 15, 2012 (edited) Which fetch array is throwing the error? My guess is the second one. EDIT: Put this at the end of your queries: or die(mysql_error()); Edited July 15, 2012 by niche Link to comment Share on other sites More sharing options...
Guest So Called Posted July 15, 2012 Share Posted July 15, 2012 You should test your query result before using it in mysql_fetch_array(). <?php$result = mysql_query("SELECT * FROM files ORDER BY RAND() LIMIT 10");if (!$result) die("1st query failed"); // test for valid $resultwhile ($row = mysql_fetch_array($result)) { $result2 = mysql_query("SELECT * FROM members WHERE id = $row[subject] "); if (!$result2) die("2nd query failed"); // test for valid $result2 while ($row2 = mysql_fetch_array($result2)) { if ($row2['public'] == "1") { $img = 'uploads/' . $row['subject'] . '/' . $row['filename']; echo '<img src="' . $img . '" style="border:0px;"/>'; } }}?> One of your queries failed. Link to comment Share on other sites More sharing options...
birbal Posted July 15, 2012 Share Posted July 15, 2012 This common errors is made sticky already. please, link back to themhttp://w3schools.invisionzone.com/index.php?showtopic=44106&view=findpost&p=245190 Link to comment Share on other sites More sharing options...
Craig Hopson Posted July 15, 2012 Author Share Posted July 15, 2012 turns out that $row['filename'] was deleted but left in database how would i stop SQL errors from showing in the future? Link to comment Share on other sites More sharing options...
niche Posted July 15, 2012 Share Posted July 15, 2012 See the EDIT on post #2 Link to comment Share on other sites More sharing options...
Craig Hopson Posted July 15, 2012 Author Share Posted July 15, 2012 (edited) thanks is there a better way to do what i'm doing? Edited July 15, 2012 by Craig Hopson Link to comment Share on other sites More sharing options...
birbal Posted July 15, 2012 Share Posted July 15, 2012 query in loop is not a good idea. you can use subquery to get the 10 random id and the select from members from the returned id from inner query. Link to comment Share on other sites More sharing options...
Craig Hopson Posted July 16, 2012 Author Share Posted July 16, 2012 how would i do this? Link to comment Share on other sites More sharing options...
guruparthi Posted June 27, 2014 Share Posted June 27, 2014 <?php$result = mysql_query("SELECT * FROM files ORDER BY RAND() LIMIT 10") or die(mysql_error());if (!$result) die("1st query failed"); // test for valid $resultwhile ($row = mysql_fetch_array($result)) { $result2 = mysql_query("SELECT * FROM members WHERE id = $row[subject] ") or die(mysql_error()); if (!$result2) die("2nd query failed"); // test for valid $result2 while ($row2 = mysql_fetch_array($result2)) { if ($row2['public'] == 1) { $img = 'uploads/' . $row['subject'] . '/' . $row['filename']; echo '<img src="' . $img . '" style="border:0px;"/>'; } }}?> Reference: http://www.phponwebsites.com/2014/03/php-mysql-fetch-array.html Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now