Search code examples
mysqlstringquotes

Storing values with single quote in MySQL


I started learning PHP coding earlier October, the issue which is causing my trouble is that my users need to store text in the database (and the text would probably contain single and double quotes). Whenever, I put a single quote in the text, it'd cause an error. I really need to store single and double quotes, otherwise the users would have errors when their text would contain phrases like: It's my car. For the meanwhile, I turn single quotes to double ones with str_replace.

The query which I use for inserting the text into the database is: INSERT INTO notes (text) VALUES ('$text')

Isn't there any fix for it?


Solution

  • You can use a function like mysql_real_escape_string to escape the string before storing it into the database.