Change the MySQL database table encoding to UTF8 with PHP
In some cases, where you forgot to change the MySQL database table encoding to UTF8 when creating but you need to insert some text such as Chinese or other Asian languages which require UTF8 into the MySQL. Also you might not have a MySQL user control interface such as phpmyadmin to make it easy for you, and so what should you do?? Let’s create a PHP file to do the job, and here is an example:
A php file named “alter.php” is created:
First we have to connect to the database:
mysql_connect("Host Name", "User Name", "User Password") or die("Connection Failed"); mysql_select_db("DataBase Name")or die("Connection Failed");
Then we create a query which is for changing the MySQL table encoding to UTF8 and the query is stored into a variable $query:
$query ="alter table tablename convert to character set utf8 collate utf8_general_ci";
Then we run the query and to see if it works, a conditional statement if,else is added.
if(mysql_query($query)){ echo "table alter to UTF8";} else{ echo "fail";}
All together we have for “alter.php” file:
<?php mysql_connect("Host Name", "User Name", "User Password") or die("Connection Failed"); mysql_select_db("DataBase Name")or die("Connection Failed"); $query ="alter table tablename convert to character set utf8 collate utf8_general_ci"; if(mysql_query($query)){ echo "table alter to UTF8";} else{ echo "fail";} ?>

