1

Everytime I make a new project, I end up in having troubles because I forgot to create the database collation by UTF-8 or there are some characters that slipped trough that I didn't see like é/à/.. but also the double .. or tripple ... seem to be very nasty. I usually use mysqli_real_escape_string to make sure he writes the characters away, and when i print them i use htlmentities. But that doesn't work for all characters, and defenitly not for double .. or tripple ... .

Is there a general rule / guideline that I should keep in mind when setting up a project, so I don't have troubles with these special characters?

j0k
  • 22,600
  • 28
  • 79
  • 90
Naruto
  • 1,210
  • 3
  • 25
  • 28

1 Answers1

1

Is there a general rule / guideline that I should keep in mind when setting up a project?

Sure.
Always set your database connection charset to match your HTML page actual charset.

Say, your pages are in utf-8, then issue

mysqli_set_charset($conn,'utf8');

right after connect

of your pages are in Windows-1251, then make it

mysqli_set_charset($conn,'cp1252');

and so on

Also always use mysqli_real_escape_string to format string literals you're adding into query dynamically,
and use htmlspecialchars() when printing user input back to HTML page

Update:
Also you need to setup your tables with charset that supports all the required characters (UTF-8 is a preferred default).

CREATE TABLE `table` (
    ...
) DEFAULT CHARSET=utf8

when creating your tables with such definition you will never have ?s in your data

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • That works for the double and tripple . , but if I now write data to my database, it is inserting every special character as an ?. – Naruto Jan 29 '13 at 09:07
  • Ok, this is what I did: I've set up my DB charset to UTF8, changed my al my db default charsets en DB charset. And now he just writes empty data (in what I believe are characters utf can't handle (Ø20)) and in the other tables he keeps writing ? in characters like é / è or à. Am I missing something? – Naruto Jan 29 '13 at 09:40