2

What's wrong with this MySQL query?

    $result = mysql_query("SELECT did FROM publications where group IN 
(SELECT s_group FROM subscriptions where uid1='$id')") or die(mysql_error());

I am getting syntax error:

        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 's_group FROM 
subscriptions where uid1='34846')' at line 1
1

3 Answers 3

3

GROUP is a reserved keyword. Surrounding with ` will signify it's a column name:

SELECT did FROM publications where `group` IN 
(SELECT s_group FROM subscriptions where uid1=1)
Sign up to request clarification or add additional context in comments.

2 Comments

Best practice I think is to always use them.
But the parser did not balk at group... it had an issue at s_group ? Suggests the OP has either not posted his exact query, or his exact error message. Beware.
1

you have to write "group" column like group, because keyword group is reserved keyword in MySQL.

$result = mysql_query("SELECT did FROM publications where `group` IN 
(SELECT s_group FROM subscriptions where uid1='$id')") or die(mysql_error());

Or you can user alias

$result = mysql_query("SELECT did FROM publications p where p.group IN 
(SELECT s_group FROM subscriptions where uid1='$id')") or die(mysql_error());

Comments

0

follow what @paulius said and then check data type of uid1 if it's number than remove quotes from $id

2 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.
I can't comment on an author's post that is why I answered here and now I can comment here.

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.