Polish characters in MySQL

Solve the garbled text problem: how to correctly set up Polish characters in MySQL — database, headers, and connection configuration.

On many forums you can come across questions like: "How do I set up Polish characters in a MySQL database? When I type Polish characters, I get garbled text???. Please help
To display Polish characters correctly, you need to remember to correctly set:

  • the encoding and string comparison method for the database (if we want to modify an existing one: CREATE => ALTER) :
  • CREATE DATABASE `db_name` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci
  • the encoding in the sent header
  • <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

The above settings should basically already work; however, in many cases the problem isn't on the MySQL server side but in the configuration of the web server itself, or PHP. If we don't have access to the above configuration, our "savior" turns out to be the ability to set the connection encoding we're interested in:
mysql_query("SET NAMES 'utf8'");
If we're still having problems we can go one step further:
mysql_query("SET NAMES `utf8` COLLATE `utf8_polish_ci`");

Finally, which is fairly obvious, we also set the editor's encoding to UTF-8, which is less obvious, with the without BOM option, especially when we use functions such as include or require.