0

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 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

1
  • The syntax highlighting in this post should give you a hint... Commented Aug 12, 2011 at 13:43

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Henkka Remove the quotes and backslashes around $categoryids as per my query.
1
$categorynameresult="SELECT DISTINCT cat_name FROM categories WHERE company = '$companyName' AND cid IN('$categoryids')";

Comments

0

Single quotes around company name causes syntax error, try this:

$categorynameresult='SELECT DISTINCT cat_name FROM categories WHERE company = \''.$companyName.'\' AND cid IN(\''.$categoryids.'\')';

1 Comment

This is wrong. The $companyName would not be evaluated in this statement because it is enclosed in a single quoted string
0

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..

Comments

0

I think you are missing the concatenate operator:

'SELECT DISTINCT cat_name FROM categories WHERE company = \'' . $companyName . '\' AND cid IN(\'' . $categoryids . '\')';

1 Comment

you definitely need single quotes around company name in sql statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.