SQLSRV order by子句中的“Array to string conversion in ..”错误

时间:2022-05-10 15:49:35

why i always get this error :

为什么我总是得到这个错误:

Array to string conversion in ..

every time i use ORDER BY clause in PHP - SQLSRV. ( i am abit new to SQLSRV so please edit my question if you think it's necessary)

每次我在PHP中使用ORDER BY子句 - SQLSRV。 (我是SQLSRV的新手,所以如果您认为有必要,请编辑我的问题)

even simple query such as

甚至简单的查询,如

SELECT * FROM tableA ORDER BY fieldA ASC

cause that error too.

导致那个错误。

if i remove the 'order by', everything works fine. below is my php sqlsrv script:

如果我删除'order by',一切正常。下面是我的php sqlsrv脚本:

$conn = "some db setting";

$query= "(SELECT * FROM tableA ORDER BY fieldA ASC)";

$queryparams = array("");

$querun = sqlsrv_query($conn,$query, $queryparams);

version i use :

我使用的版本:

php 5.4.31

SQL Server 2008

edit for more info: the query works fine if i run directly from SQL STUDIO

编辑更多信息:如果我直接从SQL STUDIO运行,查询工作正常

2 个解决方案

#1


0  

Remove the brackets / parentheses from your query string so it's:

从查询字符串中删除括号/括号,这样:

$query= "SELECT * FROM tableA ORDER BY fieldA ASC";

Having the brackets / parentheses in your query string causes a Syntax Error in SQL. Your query returns false with an array containing the error message like the following:

在查询字符串中使用括号/括号会导致SQL中出现语法错误。您的查询返回false,其中包含一个包含错误消息的数组,如下所示:

Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 156 [code] => 156 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near the keyword 'ORDER'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near the keyword 'ORDER'. ) )

I'm guessing you are trying to echo out an array, hence the Array to String conversion message.

我猜你正在尝试回显一个数组,因此Array to String转换消息。

#2


0  

thanks for the responses, i just want to share my finding regarding my own issues :/

感谢您的回复,我只想分享我对自己问题的发现:/

it seems that if we put the ORDER BY as the sub query, the query works fine. something like this :

似乎如果我们将ORDER BY作为子查询,查询工作正常。像这样的东西:

SELECT *  FROM (
SELECT a,b,c,d,e FROM tableA ORDER BY fieldA ASC
) X

i dont know wether this is a proper answer or not , but this tricky solution helps me. i am still open for more info/answer thou :)

我不知道这是否是一个正确的答案,但这个棘手的解决方案对我有帮助。我仍然愿意接受更多信息/回答你:)

#1


0  

Remove the brackets / parentheses from your query string so it's:

从查询字符串中删除括号/括号,这样:

$query= "SELECT * FROM tableA ORDER BY fieldA ASC";

Having the brackets / parentheses in your query string causes a Syntax Error in SQL. Your query returns false with an array containing the error message like the following:

在查询字符串中使用括号/括号会导致SQL中出现语法错误。您的查询返回false,其中包含一个包含错误消息的数组,如下所示:

Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 156 [code] => 156 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near the keyword 'ORDER'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near the keyword 'ORDER'. ) )

I'm guessing you are trying to echo out an array, hence the Array to String conversion message.

我猜你正在尝试回显一个数组,因此Array to String转换消息。

#2


0  

thanks for the responses, i just want to share my finding regarding my own issues :/

感谢您的回复,我只想分享我对自己问题的发现:/

it seems that if we put the ORDER BY as the sub query, the query works fine. something like this :

似乎如果我们将ORDER BY作为子查询,查询工作正常。像这样的东西:

SELECT *  FROM (
SELECT a,b,c,d,e FROM tableA ORDER BY fieldA ASC
) X

i dont know wether this is a proper answer or not , but this tricky solution helps me. i am still open for more info/answer thou :)

我不知道这是否是一个正确的答案,但这个棘手的解决方案对我有帮助。我仍然愿意接受更多信息/回答你:)