moneyman232 moneyman232 - 1 year ago 56
MySQL Question

Parsing Error with MySQL in PHP

I am currently working on a bit of code to retrieve information from a mysql server through php to be used in an android application using JSON. Unfortunately it appears i am running into errors within my PHP script`

mysql_connect("xxx","xxx","xxx"); // edited out details
$sql=mysql_query("select FName,LName from TEST where UserId=‘".$_REQUEST['userId']."‘");
while($row=mysql_fetch_assoc($sql)) $output[]=$row;

The request for the userId is supplied by a NameValuePairs variable.
the error im recieving is:

System.out(331): Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/a2275354/public_html/test.php on line 4

Anyone know what the problem is?

Answer Source

First of all you are open to SQL injection, it would be wise to learn about escaping your variables in SQL queries mysql_escape_string() will probably help you here.

And to the main issue:


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


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

Notice the differences in the characters enveloping the ".$_REQUEST['userId']."

It seems you copied the query from an RTF document of sorts so instead of ' you wrote