开发者

PHP mssql_query double quotes cannot be used

开发者 https://www.devze.com 2022-12-26 02:21 出处:网络
In java-jdbc, I can easily run the following SQL (NOTE the double quotes around columns and table names)

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..


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.


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


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

精彩评论

暂无评论...
验证码 换一张
取 消