php - 为什么我的 PDO 查询失败?

标签 php mysql pdo prepared-statement

使用 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/

相关文章:

php - PDO 代码不起作用

php - 通过 $_POST 数组循环的 PDO 插入语句

php - Codeigniter 的 `where` 和 `or_where`

PHP在mysql数据库中选择未知的和运算符

php - 即时更改 mysql 列类型

mysql - 检查单词 SQL 中间的可用字母

php - 我怎样才能使这个带有 if 语句的 PDO mysql 查询正常工作?

PHP 关闭 MySQL 连接

php - DOCUMENT_ROOT 不完整,缺少域文件夹

php - 我正在制作一个带有 map 的 PHP 游戏,我有一些问题