HomeCoding & Programming

PHP: Convert MySQL data latin1 to UTF8 character set

Like Tweet Pin it Share Share Email

Turning or convert MySQL data latin1 to utf8

 

The default character set for MySQL is latin1 (As of PHP 5.6.0, default_charset value is used as the default. From PHP 5.4.0, UTF-8 was the default, but prior to PHP 5.4.0, ISO-8859-1 was used as the default. It’s therefore a good idea to always explicitly specify UTF-8 to be safe, even though this argument is technically optional).

 

This will not support Chinese, Hindi, French and other weird multi byte character sets languages.
It will quietly support them, but returns rubbish and will cause only frustration ๐Ÿ™

 

The best character set to use Hindi and other languages is UTF-8.

Convert your database character-set from latin1 to utf8 by using the below code.

 


<?php

mysql_connect('HOSTNAME', 'DB_USER', 'DB_PASSWORD');
mysql_select_db('DATABASE_NAME');

// Set UTF-8 as the default character set for all MySQL database tables
mysql_set_charset('utf8', $link);

$res = mysql_query("SHOW TABLES");
while($row = mysql_fetch_array($res))
{
	$tablename = $row['Tables_in_'.DATABASE_NAME];
	echo $query = "ALTER TABLE $tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci";
	echo '<br>';
    mysql_query($query);
}

?>

 

 

I think the above article will help you to convert the encoding for your database tables and save your lot of affords as you have expected to converting these one by one ๐Ÿ™‚

 

Post your comments with suggestion (if you have will be always valuable), if the above one not works for you or you have any more related suggestions.