0

I have an issue. I am trying to find out if a user's email already exists in the database here is my query:

$stmt1 = "select EmailAddress from customers where EmailAddress = ' .$emailaddress. '";
 $result = $db->query($stmt1);
 if($result->num_rows === 0){

     $Err = "";

 } else {

     $Err = 'This user is already registered login instead.';

 }

What am I doing wrong? I can't seem to get num_rows to return something I can work with. Shouldn't this query return 0 if no records are found or number of rows if there is a record?

samayo
  • 16,163
  • 12
  • 91
  • 106
Dre_Dre
  • 745
  • 2
  • 6
  • 15

1 Answers1

0

Use :

$stmt1 = "SELECT EmailAddress FROM customers WHERE EmailAddress = '".$emailaddress."'";
Raphaël Vigée
  • 2,048
  • 14
  • 27
  • why *use:*?................. – Funk Forty Niner Nov 05 '15 at 20:52
  • Whats the difference between == and === ? – Dre_Dre Nov 05 '15 at 20:52
  • If the `$result->num_rows` returns you string (which is not the case) : The `'0' == 0` check would be false, whereas `0 == 0` is true. It's not very important, but according to me it looks cleaner. – Raphaël Vigée Nov 05 '15 at 20:55
  • 2
    Shouldn't i be able to use 'if($result->num_rows > 0) as well...? – Dre_Dre Nov 05 '15 at 20:55
  • Indeed ! It **MUST** ! – Raphaël Vigée Nov 05 '15 at 20:56
  • I want to do a select before an insert. The insert statement is a prepared statement and works flawlessly. The script gives no errors but jumps right to the insert statement as if the select statement didn't exist. Is there an order for prepared statements when doing a select (check if user exists before insert)? I want this handled by php not mysql. – Dre_Dre Nov 05 '15 at 21:12