Problem with SELECT querys syntax
I can not figure out what is wrong with this syntax:
$categorynameresult='SELECT DISTINCT cat_name FROM categories WHERE company = '$companyName' AND cid IN(\''.$categoryids.'\')';
I keep getting this error message:
Parse开发者_JAVA百科 error: syntax error, unexpected T_VARIABLE in ...
I know that $companyName and $categoryids have the values that I need, but there seems to be something wrong with the syntax Could sombody please help me? Thanks
You're closing the string prematurely with the single quotes around '$companyName'
, which is why PHP is giving you a parse error.
You could fix this by escaping those quotes (like you're doing with $companyids
), but variables aren't interpolated in single quoted strings anyways. Rather than simply escaping the single quotes, you need to use double quotes around the whole string:
$categorynameresult="SELECT DISTINCT cat_name FROM categories WHERE company = '$companyName' AND cid IN($categoryids)";
This assumes that $categoryids
is a comma-separated list of numeric IDs.
$categorynameresult="SELECT DISTINCT cat_name FROM categories WHERE company = '$companyName' AND cid IN('$categoryids')";
Single quotes around company name causes syntax error, try this:
$categorynameresult='SELECT DISTINCT cat_name FROM categories WHERE company = \''.$companyName.'\' AND cid IN(\''.$categoryids.'\')';
Your query is enclosed in ''
, while these also occur in the query - or, at least, try to do so.
Besides, you forgot to call mysql_real_escape()
to escape your strings to be put into the query..
I think you are missing the concatenate operator:
'SELECT DISTINCT cat_name FROM categories WHERE company = \'' . $companyName . '\' AND cid IN(\'' . $categoryids . '\')';
精彩评论