使用 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 :在第 22 行调用 C:\wamp\www\Projetv0.2\Class_SetDAO.php 中非对象的成员函数 bindParam()
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
为什么查询不起作用....
最佳答案
'sets'
未被解析为表名; SQL 认为它是一个字符串。
如果您使用的是 MySQL,请尝试将撇号切换为反引号。其他系统可能使用(ANSI 标准)双引号。
或者,完全去掉引号。 sets
不是我用过的任何 SQL 风格中的关键字,因此如果您使用任何这些风格,则根本不必引用它。
关于php - 为什么我的 PDO 查询失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12432582/