MySQL query to change character set, connection encoding, collation etc.

August 7th, 2010 by Alex Leave a reply »

This may become handy in case your database and website encoding is a bit different from what the server thought would be perfect.
Simply run this query after successful mysql_connect:

mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");

Don’t forget to adjust the encoding names according to your configs (because not every website and database uses utf8).

Additionally, it may be possible to fix your encoding bugs with a simple single query (works in every 3rd situation):


This page can be found by searching for:

mysql query charsetmysql query encoding


  1. Gail Rioz says:

    hey all, I was just checkin’ out this weblog and I really admire the idea of the article, and don’t have anything to do, so if anyone would like to to have an engrossing convo about it, please contact me on AIM, my name is heather smith

  2. NetPlayer says:

    You saved my day(s) man,though dealing for ages with queries never thought to try this.
    It’s EXTREMELY useful when dealing with different char sets between application and mysql. In my case i could only get data encoded in non-utf encoding from an old VB6 application,manipulate them on a web application, while data in mysql should be in utf actually showing greek.Your trick was the solution.
    Thank you for caring to post the snippet !

  3. Alex says:

    No problem, i spent a lot of hours dealing with different encodings too. The reason i had troubles is because i work for different persons from different countries and that was causing errors while migrating / moving websites to different hostings