PHP异常:SQLSTATE [42000]:语法错误或访问冲突:1064(PHP exception: SQLSTATE[42000]: Syntax error or access violation: 1064)

我有一个php pdo脚本,我想在“field5”中选择最高编号的记录(来自MySQL数据库中的表)。 我还有一些其他限制,见下文:

$stmt=$db->query("SELECT `field1`,`field2` FROM ".$tablename." WHERE `field3`!=".$variable1." AND `field3`!=".$variable2." AND `field4`='xx' AND `field5`<".$variable3." ORDER BY DESC `field5` LIMIT 1");

我在try语句中有很多代码,并且在我使用的catch语句中

var_dump($ex->getMessage());

从异常$ ex获取异常消息

现在,当我执行代码时,我收到以下异常消息:

'SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误; 查看与MySQL服务器版本对应的手册,以便在第3行'(长度= 232)的'DESC field5 LIMIT 1'附近使用正确的语法

对于可能出错的任何建议,我将不胜感激!

I have a php pdo script where I want to select the record (from a table in a MySQL database) with the highest number in "field5". I also have a few other constrictions, see below:

$stmt=$db->query("SELECT `field1`,`field2` FROM ".$tablename." WHERE `field3`!=".$variable1." AND `field3`!=".$variable2." AND `field4`='xx' AND `field5`<".$variable3." ORDER BY DESC `field5` LIMIT 1");

I have pretty much all the code inside a try-statement, and in the catch statement I use

var_dump($ex->getMessage());

to get the exception message from the exception $ex.

Now, when I execute the code I get the following exception message:

'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC field5 LIMIT 1' at line 3' (length=232)

I'd be grateful for any advice on what might be wrong!

最满意答案

更改

ORDER BY DESC field5

ORDER BY field5 DESC

Change

ORDER BY DESC field5

into

ORDER BY field5 DESCPHP异常:SQLSTATE [42000]:语法错误或访问冲突:1064(PHP exception: SQLSTATE[42000]: Syntax error or access violation: 1064)

我有一个php pdo脚本,我想在“field5”中选择最高编号的记录(来自MySQL数据库中的表)。 我还有一些其他限制,见下文:

$stmt=$db->query("SELECT `field1`,`field2` FROM ".$tablename." WHERE `field3`!=".$variable1." AND `field3`!=".$variable2." AND `field4`='xx' AND `field5`<".$variable3." ORDER BY DESC `field5` LIMIT 1");

我在try语句中有很多代码,并且在我使用的catch语句中

var_dump($ex->getMessage());

从异常$ ex获取异常消息

现在,当我执行代码时,我收到以下异常消息:

'SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误; 查看与MySQL服务器版本对应的手册,以便在第3行'(长度= 232)的'DESC field5 LIMIT 1'附近使用正确的语法

对于可能出错的任何建议,我将不胜感激!

I have a php pdo script where I want to select the record (from a table in a MySQL database) with the highest number in "field5". I also have a few other constrictions, see below:

$stmt=$db->query("SELECT `field1`,`field2` FROM ".$tablename." WHERE `field3`!=".$variable1." AND `field3`!=".$variable2." AND `field4`='xx' AND `field5`<".$variable3." ORDER BY DESC `field5` LIMIT 1");

I have pretty much all the code inside a try-statement, and in the catch statement I use

var_dump($ex->getMessage());

to get the exception message from the exception $ex.

Now, when I execute the code I get the following exception message:

'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC field5 LIMIT 1' at line 3' (length=232)

I'd be grateful for any advice on what might be wrong!

最满意答案

更改

ORDER BY DESC field5

ORDER BY field5 DESC

Change

ORDER BY DESC field5

into

ORDER BY field5 DESC