簡體   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