floridaflatlander Posted September 10, 2012 Share Posted September 10, 2012 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. */} Quote Link to comment https://forums.phpfreaks.com/topic/268225-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-boolean-given/ Share on other sites More sharing options...
premiso Posted September 10, 2012 Share Posted September 10, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268225-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-boolean-given/#findComment-1376760 Share on other sites More sharing options...
Jessica Posted September 10, 2012 Share Posted September 10, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/268225-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-boolean-given/#findComment-1376764 Share on other sites More sharing options...
PFMaBiSmAd Posted September 10, 2012 Share Posted September 10, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268225-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-boolean-given/#findComment-1376769 Share on other sites More sharing options...
Jessica Posted September 10, 2012 Share Posted September 10, 2012 Doh. Quote Link to comment https://forums.phpfreaks.com/topic/268225-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-boolean-given/#findComment-1376770 Share on other sites More sharing options...
floridaflatlander Posted September 10, 2012 Author Share Posted September 10, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/268225-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-boolean-given/#findComment-1376772 Share on other sites More sharing options...
Jessica Posted September 10, 2012 Share Posted September 10, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/268225-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-boolean-given/#findComment-1376774 Share on other sites More sharing options...
floridaflatlander Posted September 11, 2012 Author Share Posted September 11, 2012 Thanks all, that was the problem. Too many $r's Quote Link to comment https://forums.phpfreaks.com/topic/268225-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-boolean-given/#findComment-1377000 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.