简体   繁体   中英

Checking for email in PDO

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

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'"

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.

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

if($check->rowCount() > 0){

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM