Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I insert a special character such as ' into MySQL?

Tags:

mysql

I am executing the insert query from a shell script which reads data from multiple files. Some of the data to be inserted contains ' in the text and MySQL keeps giving errors

ERROR 1064 (42000) at line 1: 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 's Development & Empowerment, Youth Affairs                 
','
Himachal Pradesh                    ' at line 1

This is the actual text: Women's Development & Empowerment, Youth Affairs.

like image 521
charan Avatar asked Jan 26 '11 10:01

charan


People also ask

How do you add special characters to a database?

Use mysqli_real_escape_string() to Insert Special Characters Into a Database in PHP. To get user input with special characters from the form fields, we use the mysqli_real_escape_string() function. We need the following parameters: database connection and the strings we want to escape.

How do I add a character to a string in MySQL?

INSERT() function The original string, the string which will be inserted, a position of insertion within the original string and number of characters to be removed from the original string - all are specified as arguments of the function. Original string. Position of insertion within the original string.


1 Answers

You need to escape the quote, like so:

'Women\'s Development & Empowerment, Youth Affairs'

Note, that if you're generating the SQL statement from a language like PHP, there are functions available to do this for you.

In PHP, for instance, there is mysql_real_escape_string, which takes care of it for you. Note, that prepared statements are to be prefered over this, as it's harder to get those wrong.

See also:

  • The MySQL manual entry on strings
  • PHP PDO prepared statements
like image 167
Sebastian Paaske Tørholm Avatar answered Oct 20 '22 01:10

Sebastian Paaske Tørholm