UTF-8 fix – when charset is set to Latin1

Situation: you have MySQL 5 database with tables which claimed to have Latin1 charset. You filled the base (with MySQL Front, PhpMyAdmin or any other tool) with info. There IS non alnum chars (like TM, (c), long –, ellipsis etc.)

Now you want it back, and your pages are utf-8 encoded, but when you queries the base, ??’ sign returned instead of your cute symbols. Shit.

Never mind, if you can see those chars with PhpMyAdmin – I’ll help you to convert them to use them.

First, mysql5 seems to be utf-8 lover (even if other charset is claimed). So – it’s simple. Just exec SET NAMES utf8 right after database initialisation.

Second, query all the data you need into array.

Third, just do utf8_encode on data with your loved scrap

Forth, exec SET NAMES latin1

Fifth – write your data back

Sixth – tell your application to use utf8_decode when reading and utf8_encode when writing those data.

That’s all – now you can easilly transfer your data to MySQL4* or MySQL5 servers – and your app will work there!

Example:

// init skipped 
 
/*********************************** 
 * Database connect 
 **********************************/ 
$o_db = new myDB(DB::connect(DB_DSN)); 
$o_db->db_setFetchMode(DB_FETCHMODE_ASSOC); 
$o_db->db_query('SET NAMES utf8'); 
 
$q = 'SELECT page_id, page_head_title, page_head_description, page_head_keywords 
  FROM '.DB_PREFIX.'pages 
'; 
foreach ($page as $k => $p) 
  foreach (array('page_head_title', 'page_head_description', 'page_head_keywords') as $field) 
    $page[$k][$field] = utf8_encode($p[$field]); 
$o_db->db_query('SET NAMES latin1'); 
 
foreach ($page as $p) 
{ 
  $up = 'UPDATE '.DB_PREFIX.'pages 
    SET 
    page_head_title = '.$o_db->db_quote($p['page_head_title']).', 
    page_head_description = '.$o_db->db_quote($p['page_head_description']).', 
    page_head_keywords = '.$o_db->db_quote($p['page_head_keywords']).', 
    WHERE page_id = '.$o_db->db_quote($p['page_id']).' 
  '; 
  $o_db->db_query($up); 
}

*see next post

Leave a Reply