Changing character sets in a MySQL database in real time

I currently have a bunch of tables using latin1 encoding in MySQL 5.1.x database. The problem is that we recently had a lot of users trying to enter text using UTF-8 encoding, and this seemed to violate the situation.

Is it safe to blindly update a table character set? What are some best practices (besides the obvious support of everything) for such a situation?

+3
source share
2 answers

I suggest taking a look at Converting character sets into mysqlperformanceblog.

0
source

PHP, :

<?php
    $table_name = 'some_table';
    $from = 'latin1';
    $to = 'utf8';

    $q = 'SELECT * FROM '.$table_name;
    //get data from mysql table
    $data = mysql::array_qw($q) or die('Cound not select: '.mysql_error());

    for ($i = 0; $i < count($data); $i++)
    {
        $q = 'UPDATE '.$table_name.' SET ';

        foreach($data[$i] as $key=>$value)
        {
            $q.= $key.' = '."'".iconv($from, $to, mysql_real_escape_string($value))."', ";
        }
        $q[strlen($q)-2] = ' ';

        $q.= $key = "WHERE id = ".$data[$i]['id'];
        mysql_query($q) or die(mysql_error());
    }
?>

, , . ( mysql:: array_qw mysql )

0

Source: https://habr.com/ru/post/1733940/


All Articles