dqf60304 2011-03-22 10:27
浏览 71
已采纳

通过PDO的SQL和事务的使用将不会执行

I have been using PDO throughout my site; however, when it comes to updating a key in the auditkey table, the following (simplified method) will not successfully execute on the DB:

 try {
      $dbh = new PDO(etc...);
      $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

      $currentKey = getCurrentKey(); //returns a SHA1 key
      beginTransaction($dbh); // not a typo, call to a wrapper function!
      $stmt = $dbh->prepare("UPDATE auditkey SET key=:nextKey");
      $stmt->bindValue(':nextKey', sha1($currentKey), PDO::PARAM_STR);
      $success = $stmt->execute();
      $dbh->commit();
    }

The error I get is the following:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key='f6aa38e947482563913c56c2c72a59ea623c7dfb'' at line 1

When the UPDATE is amended and entered manually, the SQL works fine:

UPDATE `auditkey` SET `key`='f6aa38e947482563913c56c2c72a59ea623c7dfb';
  • 写回答

2条回答 默认 最新

  • doulingzou1712 2011-03-22 11:22
    关注

    The update query you posted at the end of your question is not an equivalent to the one you used in your prepared statement : in the hand-made query, the fields' names are enclosed in backticks.

    This shouldn't change much for the auditkey field ; but it should change a lot for the key field, as key is a reserved word.


    Try changing your prepared statement, to enclose the name of the key field in backticks :

    $stmt = $dbh->prepare("UPDATE auditkey SET `key`=:nextKey");
    


    As a reference, here is the list of Reserved words for MySQL.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳