Jump to content

mysql_fetch_array() expects parameter 1 to be resource


Craig Hopson

Recommended Posts

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

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 by niche
Link to comment
Share on other sites

Guest So Called

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

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

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

  • 1 year later...
<?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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...