Php mysql语法错误(在select查询中)报告错误

Php mysql语法错误(在select查询中)报告错误,php,mysql,Php,Mysql,下面的PHP代码报告了一个错误代码: $id = $_SESSION['sno']; $q = mysql_query("select * from messages where seen=0 and to=$id"); if(!$q){die("critical failure: ".mysql_error());} 报告的错误为: critical failure: You have an error in your SQL syntax; check the manual that co

下面的PHP代码报告了一个错误代码:

$id = $_SESSION['sno'];
$q = mysql_query("select * from messages where seen=0 and to=$id");
if(!$q){die("critical failure: ".mysql_error());}
报告的错误为:

critical failure: 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 'to=1' at line 1

“to=1”表示$\u会话['sno']设置为1

这是因为您使用的是

TO
是保留关键字,请在其周围加上反勾`以避免出现错误


由于n侧
mysql\u*
finction已被弃用,最好切换到或并使用准备好的语句以避免
mysql注入的任何风险,请在此处了解更多信息这是因为您使用的是

TO
是保留关键字,请在其周围加上反勾`以避免出现错误


由于n侧
mysql\u*
finction已被弃用,最好切换到或并使用准备好的语句,以避免
mysql注入的任何风险
,在此处了解更多信息

您必须使用`符号来表示类似的单词,因为这是我的SQL的关键字

所以您的查询看起来像

$q = mysql_query("select * from messages where seen=0 and `to`=$id");

您必须使用`符号来表示类似于的单词,因为这是MySQL的关键字

所以您的查询看起来像

$q = mysql_query("select * from messages where seen=0 and `to`=$id");

to
是保留关键字,请使用引号标识符将其转义

mysql_query("select * from messages where `seen`=0 and `to`=$id"); 

to
是保留关键字,请使用引号标识符将其转义

mysql_query("select * from messages where `seen`=0 and `to`=$id"); 

您应该使用MySQLi或PDO,而不是不推荐使用的
mysql.*
函数。您应该使用MySQLi或PDO,而不是不推荐使用的
mysql.*
函数。