SQL query that is always true?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
kri6ck
Forum Newbie
Posts: 2
Joined: Sun Mar 02, 2008 9:48 am

SQL query that is always true?

Post 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?
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

Re: SQL query that is always true?

Post 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}'") ))
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: SQL query that is always true?

Post 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.
(#10850)
Post Reply