Question: > 
> **Possible Duplicate:**  
> 
> [SET NAMES utf8 in MySQL?](https://stackoverflow.com/questions/2159434/set-names-utf8-in-mysql) 
> 
> 
> 

I am writing PHP to connect to a MySQL database. I have seen some examples that sets the encoding to UTF8 `mysql_query("SET NAMES utf8");` 

Why is this done? What are the advantages to doing this and what problems does it avoid?

Answer: 
This way the database will return data in UTF8 encoding (and expect such if you are inserting/updating). Useful if you are trying to query non-ascii characters from the server and displaying the content in general in UTF8 format, with UTF8 encoding headers. Otherwise you will see broken characters.

Especially useful if the data in the database is in non-ascii and non-utf8 encoding/collation (like CP-1251 for cyrillic) or if you have multiple collations in same database. This way you ensure that you are getting/putting all the data in single universal encoding, that you both understand and like :)

In general, it's a good practice. Most of the time you can't ensure you don't have data in non-ascii characters or there won't be such to insert. So it's just good practice to do it always, as UTF8 seems to be the widely accepted and loved data encoding these days.
The MySQL reference answers this pretty succinctly:
<http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html>
Be sure to try using google before SO :/ While I don't mind so much, many people **really** do.