kagorastidis
5/9/2017 - 9:48 AM

Converts all tables to UTF8 general Ci collation in a MySQL database

Converts all tables to UTF8 general Ci collation in a MySQL database


<?php
//change values for user, password, dbname before running

    $con = mysql_connect('localhost','user','password');
    if(!$con) { echo "Cannot connect to the database ";die();}
    mysql_select_db('database_name');
    $result=mysql_query('show tables');
    while($tables = mysql_fetch_array($result)) {
            foreach ($tables as $key => $value) {
                mysql_query("ALTER TABLE $value CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
        }}
    echo "The collation of your database has been successfully changed!";
    
?>