0

Lately, I have been writing some code for my site. Usually, I have no bother with this bit of code, but it has suddenly started having issues. I am using mysqli_fetch_array to select some values from my database, but it is returning the error:

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

I guess what we've all seen this error before, but I cannot seam to get rid of it. The code that it is referring to is as follows:

if (!empty($emailAddressGET) && !empty($passwordGET)) {

    $infoGet = mysqli_query($dbConnect, "SELECT * FROM users WHERE emailAddress = '".$emailAddress."'");

    while ($row = mysqli_fetch_array($infoGet, MYSQLI_ASSOC)) { //BUG LINE

        //Code for setting cookies, checking values etc here

    }
}

How can I prevent this error from happening?

halfer
  • 19,471
  • 17
  • 87
  • 173
Ryan
  • 1,064
  • 1
  • 15
  • 30
  • 2
    `mysqli_query` returns `FALSE on failure` which is a `boolean` value that is being passed to `mysqli_fetch_array` as the first argument (parameter). Debug your code. Also why are you checking for `$emailAddressGET` and then applying `$emailAddress` without the `GET`? – Ohgodwhy Sep 12 '14 at 22:56
  • The value with GET on the end is directly from the input. The value without is a lowered version of the value, or as the password would use sha1();. – Ryan Sep 12 '14 at 23:03
  • Voted to close, even though the duplicate is about ext/mysql instead of ext/mysqli. But the solution is the same for both. – Bill Karwin Sep 12 '14 at 23:51

1 Answers1

0

Basically if you have this problem it means your request is failing. So I suggest you debug your request step by step for instance you can change your request into something like this:

 $infoGet = mysqli_query($dbConnect, "SELECT * FROM users");

If the bug is not showing it means the error is in the condition side. In that case either the field name (emailAddress) doesn't match with the one in your table or the variable $emailAddress contains some un-sanitized information.

ZooBoole
  • 31
  • 7
  • Would anything such as: mysqli_real_escape_string(); or strtolower(); effect the code? – Ryan Sep 12 '14 at 23:24