We help IT Professionals succeed at work.

Check if an entered email address exists in 2 tables

peps03
peps03 asked
on
774 Views
Last Modified: 2012-05-28
Hi,

I have two tables, students and companies which both have a column named email. I need to check if an entered emailaddress exists in the tables.

I tried this:

$query = "select
      NOT EXISTS (select email from students where email = '$email[2]') AND
      NOT EXISTS (select email from companies where email = '$email[2]')";

      $result = mysql_query($query) or die(mysql_error());
      if (mysql_num_rows($result))

But this always returns that the email address already exists.

How should i preform this check correctly?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2010

Commented:
What are you trying to do?

If the email addy exists in neither table, then your SQL statement returns true.  If it exists in at least one of the tables, it returns false.

Author

Commented:
that is what i am trying. but it always returns false
Most Valuable Expert 2011
Author of the Year 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Great thanks.

Simple solution, thought there would be some brilliant solution to stuff it into 1 query, but lets unnecessary not complicate thing too much!
Most Valuable Expert 2011
Author of the Year 2014

Commented:
@peps03: thanks for the points.  In my experience, the quest for "brilliant solutions" sometimes complicates my life.  So instead I seek solutions that are lazy, pedestrian, simple-stupid and ordinary.  It has been a quest that has repeatedly struck gold, giving me the fastest, most accurate answers.  Thud!  I don't get points at EE by being smarter than anyone else; I get points by plodding along well-traveled paths toward well-defined goals.

All my best, ~Ray
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.