1

I don't understand why I am getting this error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

My code is:

$alreadyMember = mysql_fetch_assoc(mysql_query("SELECT id FROM members WHERE emailAddress='{$_SESSION['register']['email']}'"));

I am also getting the error here:

$alreadyRegistered = mysql_fetch_assoc(mysql_query("SELECT confirm_code FROM memberstemp WHERE emailAddress='{$_SESSION['register']['email']}'"));
StathisG
  • 1,159
  • 1
  • 10
  • 18
Jonathan
  • 15
  • 4
  • `mysql_query("SELECT id FROM members WHERE emailAddress = '{$_SESSION['register']['email']}'") or die(mysql_error());`, what does it return? And what is the value of the `$_SESSION`? – Erlesand Aug 10 '14 at 21:44
  • try storing the $_SESSION['register']['email'] in a variable and use that in the query. – ahjashish Aug 10 '14 at 21:46
  • `emailAdress` is an array of `string`s? Then I would recommend to rename it to `emailAdresses`... – Willem Van Onsem Aug 10 '14 at 21:49

2 Answers2

1

If mysql_query() fails, it returns FALSE instead of a MySQL result resource.

So basically mysql_query() is failing and you are passing FALSE to mysql_fetch_assoc() instead of the resource you are supposed to.

You have to run mysql_query() separately and check if it returns FALSE before proceeding, in which case you print mysql_error() to learn what went wrong.

Havenard
  • 27,022
  • 5
  • 36
  • 62
0

It is best to separate query and the fetching of result for easier debugging and prevent unnecessary error, like:

 $query = "SELECT id FROM members WHERE emailAddress='{$_SESSION['register']['email']}'";
 $result = mysql_query($query) or die("Error : ".mysql_error.__LINE__);
 if ($result) // Test if result has no error
    {
      $alreadyMember = mysql_fetch_assoc($result);
    }

However, it is highly recommended to use prepared statements using PDO to prevent sql injection instead of simple mysql() extensions.

Edper
  • 9,144
  • 1
  • 27
  • 46