0

I have a problem when checking if email allready exists in the database.

<?php
session_start();

include_once("connection.php");
$handler = new Connection;
$handler = $handler->connect();

$email = $_POST['email'];

$check = $handler->prepare("SELECT * FROM emailcollector WHERE email=':mail'");
$check->execute(array(':mail' => $email));


if($check->rowCount > 0){
  header("Location: yes.php");
}else{
  header("Location: no.php");
}
?>

Im using a pdo connection to the database. When i run the code it allways says that it does not exist. Im kinda new to this. Thanks for any help

Denny123
  • 13
  • 2

3 Answers3

2

In your SQL, remove the quotes around ':mail'

$check = $handler->prepare("SELECT * FROM emailcollector WHERE email=:mail");

PDO placeholder do not require any quotes, even if the value is a string. By using quotes, you are essentially doing the below, which as you can see isn't right.

SELECT * FROM emailcollector WHERE email="'mail@mail.com'"
MrCode
  • 63,975
  • 10
  • 90
  • 112
0

Possible issue with PDOStatement::rowCount():

For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action.

Reversal
  • 622
  • 5
  • 19
0

rowCount is a function and not a property. Therefore you have to call it like this, Note the ()

if($check->rowCount() > 0){
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149