Change collation for all MySQL tables to UTF-8?

Changing the collation for all tables in a MySQL database can be time consuming depending on how many tables you have.

That's why we recommend using the following PHP script for changing the collation for all tables at a time:

<?php
$db = mysql_connect('localhost','myuser_mydbuser','mypassword');
if(!$db) echo "Cannot connect to the database - incorrect details";
mysql_select_db('myuser_mydbname'); $result=mysql_query('show tables');
while($tables = mysql_fetch_array($result)) {
foreach ($tables as $key => $value) {
mysql_query("ALTER TABLE $value COLLATE utf8_general_ci");
}}
echo "The collation of your database has been successfully changed!";
?>

Make sure to substitute in the above script:

  • myuser_mydbname with your database name;
  • myuser_mydbuser with your mysql username;
  • mypassword with your password for the mysql user;
  • utf8-general_ci with your new collation if different;
  • 0 أعضاء وجدوا هذه المقالة مفيدة
هل كانت المقالة مفيدة ؟

مقالات مشابهة

What is the default mysql port?

3306 is the default mysql port.

How to connect to PostgreSQL from PHP?

For connecting to a PostgreSQL database from PHP you can use the standard pg_Connect() function.

How to import an .sql file into a Postgre database?

Log in to your cPanel and open the phpPgAdmin tool. Then click on the PostgreSQL link at the left...

How to create a Postgre user?

In order to create a PostgreSQL user you should go to your cPanel -> PostgreSQL Databases...

How to create a Postgre database?

Creating a PostgreSQL database is a straight-forward process. All you need to do is log in to...