Avatar of peps03
 asked on

Check if an entered email address exists in 2 tables


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?
PHPMySQL ServerDatabases

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Patrick Matthews

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.

that is what i am trying. but it always returns false
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

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!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Ray Paseur

@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