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.

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

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题