0

I have the following code:(php)

$alert = mysqli_query($con,"SELECT * FROM 'user_" . $row['id'] . "_notifications' LIMIT 1");

which gives me this 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 ''user_2_notifications' LIMIT 1' at line 1

please help me fix this.

2
  • 3
    Don't quote table (or column) names using '... if you need to quote them, use backticks (`)... Quote characters (') are for string literals Commented Dec 17, 2013 at 23:23
  • Which bit of it do you want help with fixing? The part where you use data as table names? The part where you apply a LIMIT to a query without specfying an ORDER BY thereby giving unpredictable results? Or the syntax error? Commented Dec 18, 2013 at 0:12

1 Answer 1

2

Get rid of the single quotes around the table name in your query:

$alert = mysqli_query($con,"SELECT * FROM user_" . $row['id'] . "_notifications LIMIT 1");

You only need to escape table names and column names if they are one of the MySQL Reserved Words. The escape character in that case is back tick (`) not single quote (') like you've in your query.

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

2 Comments

Or escape them like this: `foo`
Thank you! This worked quite well. I have actually found this in many places in my code, and I have now fixed it!

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.