繁体   English   中英

PHP mssql_query不能使用双引号

[英]PHP mssql_query double quotes cannot be used

在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不变。 还有其他解决方案吗?

谢谢尼罗什

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); 

而且效果很好

非常感谢..

它不是完全一样,但是您可以将双引号"替换为反引号:

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 ='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);

QUOTED_IDENTIFIER可能设置为OFF。

http://msdn.microsoft.com/zh-CN/library/ms174393.aspx说:

SET QUOTED_IDENTIFIER(Transact-SQL)
[...]
当SET QUOTED_IDENTIFIER为ON时,标识符可以用双引号分隔,而文字必须用单引号分隔。 and must follow all Transact-SQL rules for identifiers. 并且必须遵循所有Transact-SQL标识符规则。 有关更多信息,请参见标识符
[...]
连接时,SQL Server的SQL Server本机客户端ODBC驱动程序和SQL Server的SQL Server本机客户端OLE DB提供程序会自动将QUOTED_IDENTIFIER设置为ON。 可以在ODBC数据源,ODBC连接属性或OLE DB连接属性中进行配置。

将其设置为“ On就可以了。

暂无
暂无

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

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