PHP中MySQL解析错误


Parsing Error with MySQL in PHP

我目前正在编写一段代码,通过php从mysql服务器检索信息,以便在使用JSON的android应用程序中使用。不幸的是,我的PHP脚本`中似乎出现了错误

<?php 
mysql_connect("xxx","xxx","xxx"); // edited out details
mysql_select_db("a2275354_gtchose");
$sql=mysql_query("select FName,LName from TEST where UserId=‘".$_REQUEST['userId']."‘");
while($row=mysql_fetch_assoc($sql)) $output[]=$row;
print(json_encode($output));
mysql_close();
?>`

对userId的请求由NameValuePairs变量提供。接收到的错误是:

System.out(331):分析错误:语法错误,意外的T_ENCAPSED_AND_WHITESPACE,在4
行的/home/a275354/public_html/test.php中应为T_STRING或T_VARIABLE或T_NUM_STRING

有人知道问题出在哪里吗?

首先,您对SQL注入持开放态度,了解如何在SQL查询中转义变量是明智的。mysql_escape_string()可能会对您有所帮助。

和主要问题:

原件:

"select FName,LName from TEST where UserId=‘".$_RUEST['userId']."‘"

正确:

"select FName,LName from TEST where UserId='".$_REQUEST['userId']."'"

注意包围".$_REQUEST['userId']." 的字符的差异

您似乎是从某种RTF文档中复制了查询,所以您编写的不是'而是

是的,你使用的单引号在我看来有点可疑=UserId=‘".$_REQUEST['userId']."‘"周围的单引号——它们应该只是普通的单引号,即UserId='".$_REQUEST['UserId']."'".

您还应该避免使用$_REQUEST——而是使用$_GET或$_POST。