Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- set_time_limit(0); // for bigger databases
- $host = '';
- $user = '';
- $pass = '';
- $dbname = '';
- $new_collation = 'utf8_general_ci';
- $convert_string = 'convert(cast(convert(%s using latin1) as binary) using utf8)';
- $alter_table_and_all_columns = 'alter table %s convert to character set utf8 collate '.$new_collation;
- $conn = mysqli_connect($host,$user,$pass,$dbname);
- $sql = "SHOW TABLES";
- $result = mysqli_query($conn,$sql);
- while($row = mysqli_fetch_assoc($result)) {
- $table_list[] = $row['Tables_in_'.$dbname];
- }
- foreach($table_list as $table) {
- $res = mysqli_query($conn,"SHOW CREATE TABLE $table");
- $row = mysqli_fetch_array($res);
- #echo $row[0] . "<br>\n";
- $foo = mysqli_query($conn, "SHOW FULL COLUMNS FROM $table");
- $set = [];
- while ($row1 = mysqli_fetch_array($foo)) {
- if (!empty($row1['Collation'])) {
- # echo $row1['Field'] . ' ' . $row1['Collation'] . "\n";
- $set[] = $row1['Field'];
- }
- }
- $sql1 = "UPDATE $table SET ";
- $sets = [];
- foreach($set as $field) {
- $sets[] = $field.'='.sprintf($convert_string,$field);
- }
- $sql1 .= implode(",",$sets);
- $res1 = mysqli_query($conn,$sql1);
- if (preg_match('#CHARSET=latin1#msi', $row[1])) {
- $res2 = mysqli_query($conn, sprintf($alter_table_and_all_columns, $table));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement