0

I'm trying to check if an email address exist in the database after typing the email on the form

here is my code

$emailbox = $_POST['loginemailbox'];
$check_email =mysqli_query(" SELECT * FROM Registration WHERE Email = '$emailbox'"); 

if(mysqli_num_rows($check_email) == 1)
  {
      echo $emailbox." is found in the database :)";
  } else 
  {
    echo $emailbox."is not found in database :(";
   }

what am i doing wrong here , can anyone point out to me the error

slim
  • 59
  • 1
  • 8

3 Answers3

0

Note: PDO is best for query. As you are using mysqli so here you have to use a prepare statement for best performance and good/ smart look. I answer your question just like you are doing.

Make a database connection, use it in the query.

Try something like this:

$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// Perform queries
$check_email = mysqli_query($con, "SELECT * FROM Registration WHERE Email = '$emailbox'");
if(mysqli_num_rows($check_email)){
    echo $emailbox." is found in the database :)";
}else{
  echo $emailbox."is not found in database :(";
}

mysqli_close($con);

Output

The output is from a sqlfiddle

Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
0

I think you are missing mysqli_query($connection, $query) Try :

$connection = mysqli_connect('hostname','db_user','db_pass','db_name');
$check_email = mysqli_query($connection, "SELECT * FROM Registration WHERE Email = '$emailbox'"); 

For more detail : Mysqli_Query

Community
  • 1
  • 1
Sanchit Gupta
  • 3,148
  • 2
  • 28
  • 36
0

if you have checked after

1.adding the connection in the code

2.check table name and field name

the Please Check for

1.space before and after the email saved in database.

2.trim the email you are posting... trim($emailbox)

J. Shiv
  • 144
  • 4