I'm inserting some data into a database from a form. I'm using addslashes
to escape the text (have also tried mysql_real_escape_string
with the same result).
Regular quotes are escaped, but some other quotes are not. For example, the string:
Homer's blood becomes the secret ingredient in Moe’s new beer.
is converted to:
Homer\'s blood becomes the secret ingredient in Moe’s new beer.
I didn't think the curly quote would matter unescaped, but only this text is inserted into the database:
Homer's blood becomes the secret ingredient in Moe
So PHP thinks the curly quote is fine, but MySQL is losing the string. MySQL is not giving any errors though.
The ’ in Moe’s is the only character in your example string that wouldn't be valid if that string is latin1 encoded but your mysql server expects utf8.
Simple demonstration:
prints
Therefore the question is: Do you feed the mysql server something in a "wrong" encoding?
Each connection has a connection charset and the mysql server expects your client (php script) to send data that is encoded in that character set. You can find out what the connection charset is with
like in
This should print something like
and
character_set_connection, utf8
indicates that "my" connection character set is utf8, i.e. the mysql server expects utf8 encoded characters from the client (php). What's "your" connection charset?Then take a look at the actual encoding of your parameter string, i.e. if you had
replace that by
and check what the actual encoding of your input string is. Does it print 92 or C2 92?
I would look for a mismatch between the character encoding used in your Web interface and that used at the database level. If your Web interface uses UTF-8, for example, and your database is using the default MySQL encoding of
latin1
, then you need to set up your tables withDEFAULT CHARSET=utf8
.Use
mysql_real_escape_string()
or mysqli, by the way.addslashes()
is NOT adequate protection against SQL injection.