dongyi3776 2015-03-26 00:33
浏览 54
已采纳

PDO更新查询将不会运行,但它会在sql studio上运行

I'm converting the queries from mysql to pdo to run on mssql and I have this query that is driving me nuts:

UPDATE cms_users SET lastlogin = GETDATE() WHERE id = '1'

For some reason it breaks when I run it through the PDO on the PHP application but when I run it on the Microsoft SQL Server Management Studio it executes.

This is the line where I'm running the query:

$this->db_val->query($Query,$this->db_val) or die("Error in Update Query <br> ".$Query."<br>");

And when i added some attributes to capture the error from PDO by putting this code in:

$this->db_val->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

I got this error:

Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name 'UPDATE cms_users SET lastlogin = GETDATE() WHERE id = '1''

Not sure which is the invalid column name, i tried putting the column names in `` but that didn't work as well.

UPDATE:

If this helps, I'm getting the same issue with a different query:

DELETE FROM cms_audittrail WHERE transactiondate <= '2015-02-26'

That also works on the sql studio but is not working through the php application. I am getting:

Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name 'DELETE FROM cms_audittrail WHERE transactiondate <= '2015-02-26''

  • 写回答

1条回答 默认 最新

  • douzhanhui5662 2015-03-26 01:22
    关注

    From $this->db_val->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); , I assume the $this->db_val is the instance of PDO class or something extending it or wrapping it.

    If it's the instance of PDO, why put the $this->db_val to the PDO::query method ?

    It should just be:

    $this->db_val->query($Query);
    

    See PDO::query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分