Jump to content

mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given


floridaflatlander

Recommended Posts

I know this is the same old same old error people get but I can't figure it out.

 

I've checked my db table and there is an id_prod, id_cat & id_mem. All spelled correctly.

What's odd is I ran a test (it's commented out now) an it worked, returning the id_prod #159

 

But when I run same while loop just under it I get the mentioned error.

 

 

// Check if the form has been submitted:
if (isset($_POST['submitted'])) {

if ($_POST['sure'] == 'Yes') { 

	// get list of items to deleteing
	$q = "SELECT id_prod, id_cat FROM product WHERE id_mem = '$user' AND publish <= 1"; // Less than very important
	$r = mysqli_query ($dbc, $q) or die("Error: ".mysqli_error($dbc));

// For prob solving, mysqli_fetch_array worked here returning the id_pod #159 but not below
/*	if ($r){
	while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){
	 echo ''.$row['id_prod'].'<br />';
	}
	exit();
	} */

	if ($r && (mysqli_num_rows($r) >= 1)) {
	// delete pics of items where publish <= 1
	while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){ // <-ERROR here same while as above
		// Delete item's records and files in order of importance. From least important to most
		$q2 = "SELECT thumb, photo FROM photos WHERE id_prod = ".$row['id_prod']."";
		$r2 = @mysqli_query ($dbc, $q2);// or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
		if ($r2 && (mysqli_num_rows($r2) >= 1)){
			// needs to be a while loop, may be more than one
			while ($row2 = mysqli_fetch_array($r2, MYSQLI_ASSOC)){
			unlink('../images/members/'.$row2['thumb'].''); // Remove the photo files from folders ../images/members/
			unlink('../images/members/'.$row2['photo'].'');
			}
		}

		 // Update the publish # to record the product as deleted
		$q = "DELETE FROM product WHERE id_prod = {$row['id_prod']} AND publish <= 1";
		$r = @mysqli_query ($dbc, $q);// or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
	} // end of loop


	// When through redirect
	header("Location: $home/member/index.php?user={$_SESSION['id_mem']}");
	exit();
	} // end if records for user
	} // If someone clicks submit when no is checked
		elseif ($_POST['sure'] == 'No') {
		header("Location: $home/member/index.php?user=$user");
		exit();
	} 
} else { /* Show the form. */}

Link to comment
Share on other sites

First up, you should re-work the query where you do not have to do the loop, that will kill your performance a ton.

 

Second up,

$r2 = @mysqli_query ($dbc, $q2);// or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

 

Is suppressing any helpful errors that may be thrown. Remove the @ and see what errors are coming out of it.

Link to comment
Share on other sites

I said I'd write a good reply on this topic for the stickies, so here it is.

 

mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given

 

If you look at the manual for the mysqli_fetch_array function, you will see it expects to recieve a mysqli_result object, just like the error specifies. Instead, you have given it a boolean value. Looking at the code, you can see that you are trying to use the returned value from a mysqli_query call.

 

The manual page for mysqli_query it tells you very clearly what it returns.

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

 

So, mysqli_query returned false because your query failed. The error you're getting is caused by not checking to see if your query was successful and returned a valid mysqli_result object before trying to use that object. The manual shows examples of how to check for this, but the easiest way for debugging is:

 

$sql = "Your SQL statement here";
$result = mysqli_query($sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error(), E_USER_ERROR);

 

This will display the error message along with your entire query. You should put your queries into strings so you can easily echo them for debugging. In the long term, you should come up with more sophisticated error handling but this is a quick fix for debugging.

 

Once you have the error and the SQL statement, you can start to debug your code.

 

Edit: Updated to use trigger_error per premiso's advice, thanks!

Link to comment
Share on other sites

In this case though, the problem is the 3rd most common reason for that error, you are reusing and overwriting the $r variable inside of your loop, so after the first pass through the loop, $r no longer contains the result resource from the outermost query. Its the result from the DELETE query, which will only be a true or false.

Link to comment
Share on other sites

Thanks for the reply

 

I'm going to check that PFMaBiSmAd, I just saw that before I came back here and had just started playing with that, my SELECT is good.

 

I'll be back to mark solved

 

Good - I didn't even look at the code yet, I just wrote the generic reply. Sorry it wasn't helpful :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.