IF statement with SQL query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jcmurphy
    New Member
    • Apr 2007
    • 2

    IF statement with SQL query

    Am really beginner at this so sorry in advance.

    i have a database that i can connect to and insert data etc.
    it is an email database in which i want users to be able to add their email address. At the moment, all works well with the adding of emails.
    my problem is that if you put the users email adddress in the form again, the email address gets added again into the database.

    the script that i found does not work...
    i have worked for days just trying to get the IF statement to work correctly but cannot work out the variables?

    here it is.

    require('connec t.php');
    $form_email = ($_POST['email']);
    function emailChecker($e mail){
    global $check_result;
    $check = "SELECT id FROM esubs WHERE email = '$form_email'";
    $check_result = mysql_query($ch eck) or die(mysql_error ());
    if(!mysql_num_r ows($check_resu lt)) return false;
    else return true;
    mysql_close();
    }


    i simplified it so that if the query found something, then it printed a"found result" or if nothing was found then print "nothing Found"

    this is the script i did up.

    require('connec t.php'); // Connect to the database
    $form_email = ($_POST['email']);
    $SQL= "SELECT * FROM esubs WHERE email = '$form_email'";
    $check_result = mysql_query($SQ L);
    if(mysql_query( $SQL)) {
    print 'found result';
    } else {
    print 'Nothing found';
    }



    but no matter what email i put into the field, i get found result.
    if i put a ! in front of the mysql_query($SQ L)) {
    making it:
    if(!mysql_query ($SQL)) {

    then every time i get nothing found....

    Please help....
    thats if this makes sense....
  • ljayz
    New Member
    • Feb 2007
    • 5

    #2
    Originally posted by jcmurphy
    require('connec t.php'); // Connect to the database
    $form_email = ($_POST['email']);
    $SQL= "SELECT * FROM esubs WHERE email = '$form_email'";
    $check_result = mysql_query($SQ L);
    if(mysql_query( $SQL)) {
    print 'found result';
    } else {
    print 'Nothing found';
    }

    try this

    if(mysql_fetch_ array($check_re sult)){
    print 'found result';
    } else {
    print 'Nothing found';
    }

    i guess you miss one code mysql_fetch_arr ay

    Comment

    • jcmurphy
      New Member
      • Apr 2007
      • 2

      #3
      Originally posted by ljayz
      try this

      if(mysql_fetch_ array($check_re sult)){
      print 'found result';
      } else {
      print 'Nothing found';
      }

      i guess you miss one code mysql_fetch_arr ay

      AWESOME..... thankyou so much!!!!!!

      it pays to ask for help it seems.... could have saved me hours....
      Very much appeciated!!!!!

      Comment

      Working...