
Comprobar registro con PDO
Publicado por Jorge (69 intervenciones) el 20/10/2021 20:57:21
Hola!
Llevo todo el día probando de mil maneras diferentes y no consigo poder comprobar si existe un registro en la bd...
Con lo ultimo que he probado recibo este error:
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?' at line 1 in *****:22\nStack trace:\n#0 ****(22): PDOStatement->execute()\n#1 {main}\n thrown in **** on line 22', referer: ****
El código es este:
la conexión a la base de datos:
Llevo todo el día probando de mil maneras diferentes y no consigo poder comprobar si existe un registro en la bd...
Con lo ultimo que he probado recibo este error:
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?' at line 1 in *****:22\nStack trace:\n#0 ****(22): PDOStatement->execute()\n#1 {main}\n thrown in **** on line 22', referer: ****
El código es este:
1
2
3
4
5
6
$exists=("SELECT COUNT(*) FROM users WHERE email=?");
$stmt1=$pdo->prepare($exists);
$stmt1->bindColumn(1,$_POST["email"],PDO::PARAM_STR);
$result=$stmt1->execute();
if($result->fetchColumn() > 0){
setcookie("email_exists","1",time() + 3600, "/");
la conexión a la base de datos:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$dsn = "mysql:host=localhost;port=3306;dbname=***";
$username = "***";
$password = "***";
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4'",
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
);
try {
$pdo = new PDO($dsn, $username, $password, $options);
} catch (PDOException $e) {
echo $e->getMessage();
die();
}
Valora esta pregunta


0