我有一个pdo准备好的声明,但不起作用。你能帮我么?我将包括一些代码和错误。 不要太注意包含的database.php文件。它只存储新PDO所需的变量。
include('mysql/database.php');
$dbcheck_email_validation=$_POST["email_validation_ajax"];
$dbcheck_email_validation=htmlspecialchars($dbcheck_email_validation);
$dbcheck_email_validation=mysql_real_escape_string($dbcheck_email_validation);
$sql_query = "SELECT * FROM users WHERE email = ?";
$query = $create_pdo->prepare($sql_query);
$query->execute(array($dbcheck_email_validation));
$query->setFetchMode(PDO::FETCH_ASSOC);
$row_from_query = $query->fetch();
$dbcheck_email_validation_assoc=$row_from_query['email'];
echo json_encode($dbcheck_email_validation_assoc);
错误
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO)' in /home/letsrate/public_html/favorit/ajax/ajax_email_validation.php:6Stack trace:#0 /home/letsrate/public_html/favorit/ajax/ajax_email_validation.php(6): PDO->__construct('mysql:host=;dbn...', NULL, NULL)#1 {main} thrown in /home/letsrate/public_html/favorit/ajax/ajax_email_validation.php on line 6
答案 0 :(得分:0)
删除此行:
$dbcheck_email_validation=mysql_real_escape_string($dbcheck_email_validation);
您无法在PDO中使用mysql
功能。
答案 1 :(得分:0)
仅为了代码DRYness
include('mysql/database.php');
$query = $create_pdo->prepare("SELECT email FROM users WHERE email = ?");
$query->execute(array($_POST["email_validation_ajax"]));
echo json_encode($query->fetchColumn());
这是您真正需要的所有代码。