PHP mssql_query 不能使用双引号

发布于 2024-08-27 21:30:18 字数 1136 浏览 5 评论 0原文

在 java-jdbc 中,我可以轻松运行以下 SQL(注意列和表名称周围的双引号),

Select 
       cus."customer_id" ,
       cus."organisation_or_person" ,
       cus."organisation_name" ,
       cus."first_name" ,
       cus."last_name" ,
       cus."date_became_customer" ,
       cus."other_customer_details"
From 
      "Contact_Management"."dbo"."Customers"    cus

但 PHP 中的相同查询会出错,提示无效语法

“警告:mssql_query() [function.mssql-query]:消息: 'customer_id' 附近的语法不正确(严重性 15)”

但如果删除所有双引号,查询将正常工作并且不会出现错误。

该查询是从 java 应用程序移植的,因此我想保留双引号和 SQL 原样。还有其他解决方案吗?

谢谢 Nilesh

Volkerk -- 解决方案(SET QUOTED_IDENTIFIER ON)

我执行了以下操作

    $sql = <<<EOD
Select 
       cus."customer_id" ,
       cus."organisation_or_person" ,
       cus."organisation_name" ,
       cus."first_name" ,
       cus."last_name" ,
       cus."date_became_customer" ,
       cus."other_customer_details"
From 
      "Contact_Management"."dbo"."Customers"    cus
EOD;

$db->Execute('SET QUOTED_IDENTIFIER ON');
    $rs = $db->Execute($sql); 

,效果非常好,

非常感谢..

In java-jdbc, I can easily run the following SQL (NOTE the double quotes around columns and table names)

Select 
       cus."customer_id" ,
       cus."organisation_or_person" ,
       cus."organisation_name" ,
       cus."first_name" ,
       cus."last_name" ,
       cus."date_became_customer" ,
       cus."other_customer_details"
From 
      "Contact_Management"."dbo"."Customers"    cus

But the same query in PHP errors out saying invalid syntax

"Warning: mssql_query() [function.mssql-query]: message: Incorrect syntax near 'customer_id'. (severity 15) "

But If remove all the double quotes, the query works fine and no errors.

The query is ported from a java application so I would like to keep the double quotes and the SQL as it is. Any alternative solutions?

Thank you
Nilesh

Volkerk -- Solution (SET QUOTED_IDENTIFIER ON)

I did the following

    $sql = <<<EOD
Select 
       cus."customer_id" ,
       cus."organisation_or_person" ,
       cus."organisation_name" ,
       cus."first_name" ,
       cus."last_name" ,
       cus."date_became_customer" ,
       cus."other_customer_details"
From 
      "Contact_Management"."dbo"."Customers"    cus
EOD;

$db->Execute('SET QUOTED_IDENTIFIER ON');
    $rs = $db->Execute($sql); 

And it worked perfect

Thank you so much..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

來不及說愛妳 2024-09-03 21:30:18

QUOTED_IDENTIFIER 可能设置为 OFF。

http://msdn.microsoft.com/en-us/library/ms174393。 aspx 说:

SET QUOTED_IDENTIFIER (Transact-SQL)
[...]

当 SET QUOTED_IDENTIFIER 为 ON 时,标识符可以用双引号分隔,文字必须用单引号分隔。 当 SET QUOTED_IDENTIFIER 为 OFF 时,标识符不能加引号,并且必须遵循所有关于标识符的 Transact-SQL 规则。有关详细信息,请参阅标识符
[...]

连接时,SQL Server Native Client ODBC 驱动程序和 SQL Server SQL Server Native Client OLE DB 提供程序会自动将 QUOTED_IDENTIFIER 设置为 ON。这可以在 ODBC 数据源、ODBC 连接属性或 OLE DB 连接属性中进行配置。 对于来自 DB-Library 应用程序的连接,SET QUOTED_IDENTIFIER 的默认设置为“关闭”。

将其设置为“打开”,然后就可以开始了。

QUOTED_IDENTIFIER is probably set to OFF.

http://msdn.microsoft.com/en-us/library/ms174393.aspx says:

SET QUOTED_IDENTIFIER (Transact-SQL)
[...]

When SET QUOTED_IDENTIFIER is ON, identifiers can be delimited by double quotation marks, and literals must be delimited by single quotation marks. When SET QUOTED_IDENTIFIER is OFF, identifiers cannot be quoted and must follow all Transact-SQL rules for identifiers. For more information, see Identifiers
[...]

The SQL Server Native Client ODBC driver and SQL Server Native Client OLE DB Provider for SQL Server automatically set QUOTED_IDENTIFIER to ON when connecting. This can be configured in ODBC data sources, in ODBC connection attributes, or OLE DB connection properties. The default for SET QUOTED_IDENTIFIER is OFF for connections from DB-Library applications.

set it to On and you're good to go.

不疑不惑不回忆 2024-09-03 21:30:18

它并不完全按原样,但您可以用反引号替换双引号 "

Select 
       cus.`customer_id` ,
       cus.`organisation_or_person` ,
       cus.`organisation_name` ,
       cus.`first_name` ,
       cus.`last_name` ,
       cus.`date_became_customer` ,
       cus.`other_customer_details`
From 
      `Contact_Management`.`dbo`.`Customers`    cus

It's not exactly as-is, but you could replace the double-quotes " with backticks:

Select 
       cus.`customer_id` ,
       cus.`organisation_or_person` ,
       cus.`organisation_name` ,
       cus.`first_name` ,
       cus.`last_name` ,
       cus.`date_became_customer` ,
       cus.`other_customer_details`
From 
      `Contact_Management`.`dbo`.`Customers`    cus
甜妞爱困 2024-09-03 21:30:18

这又如何呢?

$query ='Select 
   cus."customer_id" ,
   cus."organisation_or_person" ,
   cus."organisation_name" ,
   cus."first_name" ,
   cus."last_name" ,
   cus."date_became_customer" ,
   cus."other_customer_details"
From 
  "Contact_Management"."dbo"."Customers"    cus';

$query = str_replace('"', '', $query);

What about this?

$query ='Select 
   cus."customer_id" ,
   cus."organisation_or_person" ,
   cus."organisation_name" ,
   cus."first_name" ,
   cus."last_name" ,
   cus."date_became_customer" ,
   cus."other_customer_details"
From 
  "Contact_Management"."dbo"."Customers"    cus';

$query = str_replace('"', '', $query);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文