Reputation: 13692
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....
Upvotes: 1
Views: 713
Reputation: 86585
'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.
Upvotes: 2
Reputation: 5757
Column & table names should be encapsulated in (`)
INSERT INTO `sets` () VALUES ()
Upvotes: 1