2

Possible Duplicate:
UTF-8 all the way through

I am scraping data with curl from a web which have word Café. It looks fine when i echo it but after insert in mysql data base it become Café
How to handle it.

Community
  • 1
  • 1
Sohail Ahmed
  • 1,667
  • 14
  • 23
  • i am using default setting of wamp. In table structure Collation = latin1_swedish_ci – Sohail Ahmed Oct 25 '12 at 08:03
  • [Handling Unicode Front To Back In A Web App](http://kunststube.net/frontback/) – deceze Oct 25 '12 at 09:39
  • This question is literally asked several times per day here. Please do some initial research. – deceze Oct 25 '12 at 09:41
  • But specifically this person is working with someone else's data. A simple answer of utf8_encode($string) would suffice to ensure it gets inserted into his mysql correctly. The related thread is a great guideline as to ensuring UTF-8 consistency, but in reality the way the web is structured, we're constantly trading and manipulating data across computers, so in reality he's asking for a conversion. – ericjam Feb 23 '15 at 21:27

2 Answers2

2

PHP: internal encoding, file encoding

Runtime configuration:

mb_internal_encoding("UTF-8");

And set the file encoding in you text editor to UTF8 without BOM (for example in Notepad++: Encoding menu, then Convert to UTF8 without BOM)

MySQL: table, column encoding/collation

Set all encoding and collation to UTF8 in you editor/designer (PhpMyAdmin).

Between them: connection encoding/collation

I don't know what driver are you using, for mysqli (and for mysql but please don't use that):

"SET CHARACTER SET 'utf8'"
'SET NAMES utf8'

PDO has it's own configuration for this, see php.net.

Response encoding

Set it with a header call:

header('Content-Type: text/html; charset=utf-8');

Peter Kiss
  • 9,309
  • 2
  • 23
  • 38
  • where to set them? are you talking about table structure? i am using default setting of wamp. In table structure Collation = latin1_swedish_ci. Also plz sagest only one field in table set to utf8 or whole table? – Sohail Ahmed Oct 25 '12 at 08:08
  • 1
    @DarkWish any text fields need to be set to support the character encoding you expect it to have in it. If you expect every text field to have utf8 in it then you need to tell each text field to use the correct collation. – Jon Taylor Oct 25 '12 at 08:14
  • 1
    Peter, please reword your answer, it is extremely poor quality. – Jon Taylor Oct 25 '12 at 08:15
  • I have now using utf8_unicode_ci but no success. Set all PHP: internal encoding, table, column encoding/collation to utf8_unicode_ci – Sohail Ahmed Oct 25 '12 at 08:33
1

There are several things you neecd to check. First check that the encoding on the table you are using is capable of supporting any special characters you need. Use something like utf-unicode-ci. I would suggest reading up a little on these.

There are several utf8 character encodings, they are optimized for searching in different languages, hence their names. For more on unicode charsets read here

You must also make sure your connection knows which character encoding you are using in php.

Jon Taylor
  • 7,865
  • 5
  • 30
  • 55