Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing values with single quote in MySQL [duplicate]

Tags:

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?

like image 818
Areeb Avatar asked Nov 29 '14 15:11

Areeb


People also ask

Does MySQL use single or double quotes?

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.

Do single or double quotes matter in SQL?

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.

Can I use double quotes in MySQL?

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.


2 Answers

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

like image 55
Jakub Arnold Avatar answered Sep 19 '22 03:09

Jakub Arnold


Please use any of the following concepts:

  1. Use the QUOTE() function
  2. Use escaping
like image 28
abhijitcaps Avatar answered Sep 18 '22 03:09

abhijitcaps