简体   繁体   English

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

[英]PDO update query won't run but it runs on the 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: 我正在将查询从mysql转换为pdo以便在mssql上运行,而我却受到以下查询的困扰:

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. 由于某种原因,当我通过PHP应用程序上的PDO运行它时,它会中断,但是当我在Microsoft SQL Server Management Studio上运行它时,它就会执行。

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: 当我添加一些属性以通过将以下代码放入PDO捕获错误时:

$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'' 消息为'SQLSTATE [42S22]的未捕获异常'PDOException':[Microsoft] [SQL Server本机客户端11.0] [SQL Server]无效的列名'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. 这也适用于sql studio,但不适用于php应用程序。 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'' 消息为'SQLSTATE [42S22]的未捕获异常'PDOException':[Microsoft] [SQL Server本机客户端11.0] [SQL Server]无效的列名'DELETE FROM cms_audittrail WHERE transactiondate <='2015-02-26”

From $this->db_val->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $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. ,我假设$this->db_valPDO类的实例,或者是扩展它或包装它的东西。

If it's the instance of PDO , why put the $this->db_val to the PDO::query method ? 如果是PDO的实例,为什么要将$this->db_val放在PDO::query方法中?

It should just be: 应该只是:

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

See PDO::query . 参见PDO :: query

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM