【问题标题】:Why does my PDO query fail?为什么我的 PDO 查询失败?
【发布时间】:2012-09-14 22:06:31
【问题描述】:

使用 WampServer 和 phpMyAdmin。

代码如下:

function createRecord(){
 $id=null;
 var_dump("ID: ",$id);
 $length=0;
 $dbh=$this->dbConnect();
 var_dump("DBH: ", $dbh);
 $dbh->beginTransaction();
 //try{
  var_dump("DBH: ", $dbh);
  //$dbh->beginTransaction();
  $insertInSets=$dbh->prepare("INSERT INTO 'sets' () VALUES ()"); // PDO Statement object.  ID is generated via autoincrement.
  var_dump("InsertInSets: ", $insertInSets);
  $id=$dbh->lastInsertId();
  var_dump("ID: ",$id);
  $insertInClosures=$dbh->prepare("INSERT INTO 'closures' (ancestor,descendant,length) VALUES (:ancestor,:descendant,:length)");
  var_dump("InsertInClosures: ", $insertInClosures);
  $insertInClosures->bindParam(":ancestor", $id); //<- This is line 22
  $insertInClosures->bindParam(":descendant", $id);
  $insertInClosures->bindParam(":length", $length);
  //$dbh->commit();
  exit;

我尝试了使用和不使用try 和事务。无论如何,我都会得到以下信息:

Fatal error: Call to a member function bindParam() on a non-object in C:\wamp\www\Projetv0.2\Class_SetDAO.php on line 22

var_dumps 的输出如下:

string 'ID: ' (length=4)
null

string 'DBH: ' (length=5)
object(PDO)[8]

string 'DBH: ' (length=5)
object(PDO)[8]

string 'InsertInSets: ' (length=14)
boolean false

string 'ID: ' (length=4)
string '0' (length=1)

string 'InsertInClosures: ' (length=18)
boolean false

为什么查询不起作用....

【问题讨论】:

    标签: php mysql pdo prepared-statement


    【解决方案1】:

    'sets' 没有被解析为表名; SQL 认为它是一个字符串。

    如果您使用的是 MySQL,请尝试将撇号切换为反引号。其他系统可能会使用(ANSI 标准)双引号。

    或者,完全去掉引号。 sets 不是我用过的任何 SQL 风格的关键字,所以如果你使用这些风格中的任何一种,你根本不必引用它。

    【讨论】:

      【解决方案2】:

      列名和表名应该用(`)封装

      INSERT INTO `sets` () VALUES ()
      

      【讨论】:

        猜你喜欢
        • 2011-05-24
        • 1970-01-01
        • 1970-01-01
        • 2012-07-25
        • 2011-02-26
        • 2015-04-13
        • 1970-01-01
        • 2017-09-23
        相关资源
        最近更新 更多