-1

Everything in my new website is working just fine, except that I can't get this piece of code to work:

$query = mysql_query("SELECT * FROM members WHERE useremail = '$useremail'");
$row = mysql_fetch_assoc($query);
$activation = $row['activation'];
if ($activation == '0') {
    die('Your account is not yet active. Please check your email!');
    exit();
}

In the database, the type is enum('0', '1') and the field name is activation.

Here is the error message I am getting:

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

Could someone please give me a hint?

Thank you.

4
  • echo actual sql code, copy it to querybrowser and see the result.
    – k102
    Nov 18, 2011 at 7:10
  • Try: mysql_query("SELECT * FROM members WHERE useremail = '$useremail'") or die(mysql_error());
    – dfsq
    Nov 18, 2011 at 7:11
  • Please tell me you're escaping $useremail Nov 18, 2011 at 7:11
  • @Carlos can you post your DB Structure ?
    – balaphp
    Nov 18, 2011 at 7:13

3 Answers 3

0

The error message suggests that your query is invalid:

$query = mysql_query("SELECT * FROM members WHERE useremail = '$useremail'");

Are you doing any input sanitization for $usermail?

Are you sure your database contains that table and that the table contains that column.

Try doing a little debugging:

$query = "SELECT * FROM members WHERE useremail = '$useremail'";
echo $query;

and try running the content of $query directly in your database (from phpMyAdmin or something).

1
  • @Carlos what do you mean it connects fine? Have you tried what I suggested? Nov 18, 2011 at 7:22
0

Everything in my new website is working just fine

...until something goes wrong.

You have to learn how to handle errors.

run all your queries at least this way.

$query  = "SELECT * FROM members WHERE useremail = '$useremail'"
$result = mysql_query() or trigger_error(mysql_error()." ".$query);

and you always be notified of any error and it's reason

or implement any other way of error notifications, employing mysql_error() function which is the only thing in the world that can tell you where the actual problem is.

-1

Change your first line to:

$query = mysql_query("SELECT * FROM members WHERE useremail = '$useremail'") or die(mysql_error());

And it will spit out the mysql error that is causing the Warning =)

You should not be presuming the call to mysql_query returns a valid result; It's good practice to always test the result first, e.g.

$r=mysql_query("SELECT * YADYADA");
if($r && mysql_num_rows($r)>0){
  $row=mysql_fetch_assoc($r);
}else{
  //Uh oh, something's not right~~~ throw an exception maybe?
}
4
  • why exception for empty result ? Nov 18, 2011 at 7:18
  • @Col.Shrapnel I didn't actually throw and exception, I was just hinting at one of the options on the table. An Exception would at least be a step more elegant than his die() call ;)
    – Shad
    Nov 18, 2011 at 7:21
  • i see the problem now. Thank you.
    – Carlos
    Nov 18, 2011 at 7:22
  • well, your answer is just an offtopic. as it seems it will throw exceptions forever. Nov 18, 2011 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.