我的PDO预备声明出了什么问题?

时间:2013-02-25 19:11:09

标签: php arrays pdo

显然,我正在准备陈述错误,但我不确定我做错了什么。

这两个代码段是相同的,第二行的外。

这失败了:

    $dbh = new PDO('mysql:host=localhost;dbname=' . $DB_Database, $DB_UserName, $DB_Password);
    $sth = $dbh->prepare("SELECT * FROM `PNB`.`Users` WHERE `Users`.`EMail` = :email OR `Users`.`Temp_EMail` = :temp_email");
    $sth->execute(array(':email' => $email, ':temp_email' => $email));
    $sth->setFetchMode(PDO::FETCH_ASSOC);
    $res = $sth->fetch();
    $dbh = null;

这项硬编码测试有效:

    $dbh = new PDO('mysql:host=localhost;dbname=' . $DB_Database, $DB_UserName, $DB_Password);
    $sth = $dbh->prepare("SELECT * FROM `PNB`.`Users` WHERE `Users`.`EMail` = 'me@example.com' OR `Users`.`Temp_EMail` = 'me@example.com'");
    $sth->execute(array(':email' => $email, ':temp_email' => $email));
    $sth->setFetchMode(PDO::FETCH_ASSOC);
    $res = $sth->fetch();
    $dbh = null;

我做错了什么?

谢谢!

更新:已解决!

确切的问题仍然未知,但似乎与用户“你的常识”在下面的评论中建议的“过度命名”有关。

这很好用:

$dbh = new PDO('mysql:host=localhost;dbname=' . $DB_Database, $DB_UserName, $DB_Password);
$sth = $dbh->prepare("SELECT * FROM Users WHERE EMail=:email OR Temp_EMail=:temp_email");
$sth->execute(array(':email' => $email, ':temp_email' => $email));

感谢大家。我学到了很多并解决了这个问题。

给你常识的信息;如果您将评论形成为“答案”,那么我可以接受它。

3 个答案:

答案 0 :(得分:0)

很难回答。
你的代码对我来说似乎没问题。因此,调试似乎是唯一的方法。

  

我做错了什么?

始终从您的PDO中提出这个问题 每次连接到PDO时,都要这样做(同时确保您可以在屏幕上或记录中看到错误):

error_reporting(E_ALL);
$opt = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$dsn = 'mysql:host=localhost;dbname=' . $DB_Database;
$dbh = new PDO($dsn, $DB_UserName, $DB_Password, $opt);

如果有错误 - 您会收到通知 如果没有 - 检查拼写错误的问题。

答案 1 :(得分:0)

快速尝试 - 你能用这两条线做到这一点吗?

$sth = $dbh->prepare("SELECT * FROM `PNB`.`Users` WHERE `Users`.`EMail` = :email OR `Users`.`Temp_EMail` = :temp_email");
$sth->execute(array(':email' => 'me@example.com', ':temp_email' => 'me@example.com'));

换句话说......你设置了你的$ email变量吗?

答案 2 :(得分:0)

试试这个

  $dbh = new PDO('mysql:host=localhost;dbname=' . $DB_Database, $DB_UserName, $DB_Password);
$sth = $dbh->prepare("SELECT * FROM `PNB`.`Users` WHERE `Users`.`EMail` = :email OR `Users`.`Temp_EMail` = :temp_email");
$sth->bindParam(':email', $email, PDO::PARAM_STR);
$sth->bindParam(':temp_email', $email, PDO::PARAM_STR);
$sth->execute();
$res = $sth->fetch(PDO::FETCH_ASSOC);
$dbh = null;