6

I'm parsing some data by using PHP and putting it into MySQL. But if the data contains Special Characters like êm-Khê MySQL is outputting following error:

SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xEAm-Kh\xEA...'

After i test, MySQL is not the problem. (Table and Column Collation is utf8_general_ci) When i INSERT that êm-Khê Strings into the Table DIRECTLY (manually), it goes into it. So MySQL can accept that data.

So any idea why it is having this error at PHP level?

I do not understand and having about encoding knowledge very well.

  • Why is it ê transformed into \xEA in the query?

So when i get the data like ê, how can i put it into the Database as it is ê unchanged?

2

3 Answers 3

3

So the problem is that your db connection is not UTF8 but probably LATIN1 so you have to either use PHP's function utf8_encode() or execute SET NAMES utf8 after you connect to the database.

$handle = new PDO("mysql:host=localhost;dbname=dbname",
'username', 'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

This thread has some good references and explanations

3
  • PDO? Sorry i am not getting what do you mean. I am using mysql_connect. It is not like that. Oct 5, 2012 at 5:54
  • 1
    Oh, OK i got it by using php utf8_encode() method. Thanks x) Oct 5, 2012 at 7:13
  • It's good to mention that after 5.3.6 you should use ;charset=utf8 as part of the connection string.
    – Ja͢ck
    Oct 5, 2012 at 8:55
0

My guess is that there's an encoding mismatch between PHP and MySQL. ê is not really "transformed" into \xEA, it's just that MySQL does not know what the byte sequence EA is supposed to be and therefore just complains about the raw bytes being wrong. You need to first know what encoding the strings are in in PHP; which depends on where they are coming from. Then you need to set the correct MySQL connection encoding, so MySQL knows what encoding the strings you're sending it are in. How to do that depends on how exactly you connect to the database.

See Handling Unicode Front To Back In A Web App for a rundown of the whole process.

2
  • Hi, the source encoding of string in Php is utf-8 and it is coming from XML. I mean, the XML is encoded as utf-8. Oct 5, 2012 at 5:52
  • Then double check that using bin2hex on the query you are sending to the database, and check what your connection encoding is set to.
    – deceze
    Oct 5, 2012 at 5:55
-1

you can use this function. mysql_real_escape_string($user), mysql_real_escape_string($password));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.