Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to insert of search or read for data with the ' marks in and am reciving an error about MySQL syntax, how can I use the ' in a query?
Posted
Comments
Richard C Bishop 8-Feb-13 16:08pm    
You will need to post the code and error message you are receiving.

Everything is shown in MySQL documentation; was to so hard to consult it?

This is how to escape ' " and other characters: http://dev.mysql.com/doc/refman/5.0/en/string-literals.html[^].

However, it's possible that the root problem is different. Do you obtain SQL query by concatenating some SQL language constructs with data? Even if it works, this is not a right thing to do. You should better use parametrized statements instead. Please see:
http://msdn.microsoft.com/en-us/library/ms254953.aspx[^],
http://msdn.microsoft.com/en-us/library/yy6y35y8%28v=vs.80%29.aspx[^].

Not only it's generally better and will help you to avoid escaping problems, it's also much safer, which is very important. In particular, it can help you to get protected from a well-known exploit called SQL injection:
http://en.wikipedia.org/wiki/SQL_injection[^].

Note the section 3.1 explaining the importance of parametrized statements.

—SA
 
Share this answer
 
"Don't!" Is the simple answer.

Use parameterized queries instead - that way you will also not be vulnerable to SQL injection attacks, which can damage or destroy your database.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Feb-13 16:21pm    
Correct, a 5, but I also referenced escaping and links on parametrized queries and SQL injection.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900