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?
This function in MySQL is used to return a result that can be used as a properly escaped data value in an SQL statement. The string is returned enclosed by single quotation marks and with each instance of backslash (\), single quote ('), ASCII NULL, and Control+Z preceded by a backslash.
Single quotes are used to indicate the beginning and end of a string in SQL. Double quotes generally aren't used in SQL, but that can vary from database to database. Stick to using single quotes. That's the primary use anyway.
Double quotes are supported by MySQL for string values as well, but single quotes are more widely accepted by other RDBMS, so it is a good habit to use single quotes instead of double.
You can use a function like mysql_real_escape_string
to escape the string before storing it into the database.
Please use any of the following concepts:
QUOTE()
functionIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With