Page 1 of 1

SQL query that is always true?

Posted: Tue Mar 04, 2008 8:47 am
by kri6ck
I have this query to find out whether the email address the user typed in, already exists in the database.

if($res=mysql_query("SELECT address FROM email WHERE address ='{$new_emailaddress}'") <>FALSE) {
echo "This email address already exists . Enter a new one.";}


the problem is, the query always returns true. But I want to compare it to the email address the user enters.

Can anyone help me?

Re: SQL query that is always true?

Posted: Tue Mar 04, 2008 4:52 pm
by nincha
Because mysql_query always returns a resource id. Try this:

if($myRow = mysql_fetch_assoc(mysql_query("SELECT address FROM email WHERE address ='{$new_emailaddress}'") ))

Re: SQL query that is always true?

Posted: Tue Mar 04, 2008 6:55 pm
by Christopher

Code: Select all

$result = mysql_query("SELECT address FROM email WHERE address ='{$new_emailaddress}'");
if(mysql_num_rows($result) > 0) {
    $myRow = mysql_fetch_assoc($result);
}
You should also check for errors.