注: 私の質問は、クエリが失敗する理由ではありません。
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
私の質問:
クエリが実行されていないのに、設定した0forの値が取得されるのはなぜですか?通常はクエリが失敗すると、この値になるはずです。$idnullfalse