Convert the character set of a mysql database

This php-script is for converting content of all tables of a mysql database to another character set. In this case i convert everything (latin 8859-1) to utf8_swedish_ci. The same action can be made from the shell with mysqldump and iconv, but the programmer in me prefers the-php-way. As always, tweaks and comments are very helpful.

<?

set_time_limit(0);

$link = mysql_connect(’localhost’, ’mysql_user’, ’mysql_password’);

mysql_select_db(’db’);

$sql = "SHOW TABLES FROM db";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
{
$sql = "ALTER TABLE ".$row[0]." CONVERT TO CHARACTER SET utf8 COLLATE utf8_swedish_ci";
mysql_query($sql);
echo ’Converting table: ’.$row[0].'<br>’;
}

?>

Lämna ett svar

E-postadressen publiceras inte. Obligatoriska fält är märkta *

Denna webbplats använder Akismet för att minska skräppost. Lär dig hur din kommentardata bearbetas.