简体   繁体   中英

Why does my PDO query fail?

Using WampServer and phpMyAdmin.

Here is the code:

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;

I tried both with and without the try and the transaction. In any case I get the following:

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

And the outputs from the var_dumps is as follows:

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

Why are the queries not working....

'sets' is not being parsed as a table name; SQL considers it a string.

If you're using MySQL, try switching the apostrophes to backquotes. Other systems might use (ANSI-standard) double-quotes.

Or, get rid of the quotes entirely. sets is not a keyword in any flavor of SQL that i've ever used, so if you're using any of those flavors, you don't have to quote it at all.

列和表名应封装在(`)中

INSERT INTO `sets` () VALUES ()

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