このPHPスクリプトは、既存のデータベースとテーブルがUTF-8に変換することができます:、ファイル上に保存
<?php
// Database connection details
$db_srv = 'localhost';
$db_usr = 'user';
$db_pwd = 'password';
$db_name = 'database name';
// New charset information, update accordingly if not UTF8
$char_set = 'utf8';
$char_collation = 'utf8_general_ci';
header('Content-type: text/plain');
// extablish the connection
$connection = mysql_connect($db_srv,$db_usr,$db_pwd) or die(mysql_error());
// select the databse
$db = mysql_select_db($db_name) or die(mysql_error());
// get existent tables
$sql = 'SHOW TABLES';
$res = mysql_query($sql) or die(mysql_error());
// for each table found
while ($row = mysql_fetch_row($res))
{
// change the table charset
$table = mysql_real_escape_string($row[0]);
$sql = " ALTER TABLE ".$table."
DEFAULT CHARACTER SET ".$char_set."
COLLATE ".$char_collation;
mysql_query($sql) or die(mysql_error());
echo 'The '.$table.' table was updated successfully to: '.$char_set."\n";
}
// Update the Collation of the database itself
$sql = "ALTER DATABASE CHARACTER SET ".$char_set.";";
mysql_query($sql) or die(mysql_error());
echo 'The '.$db_name.' database collation';
echo ' has been updated successfully to: '.$char_set."\n";
// close the connection to the database
mysql_close($connection);
?>
あなたのウェブサイトのルートにアップロードし、db_charset.php
を言うことができますし、ブラウザからそれを実行します。
例えば、
http://www.yoursite.com/db_charset.php
その他の考慮事項は、変換後に正常に動作し、すべてを持っていることが必要です:
- UTF-8
- PHPのヘッダとHTMLヘッダもに設定する必要がでエンコードする必要があります使用されているPHPファイルUTF-8
チェックこのページ、誰かが私はhttp://stackoverflow.com/questions/9703536/bad-characters-when-printing-text-前にそれを使用していたとして、代わりにセット名のmysql_set_charsetを使用するように私を提案from-utf8-unicode-ci-mysql-table –
私はこれを使用しており、完全に正常に動作します。と私は両方を使用して、名前を設定し、彼が言うように文字セットを設定します。 – guybennet
実際に問題は印刷されたテキストではなく、問題はmysqlに格納されたテキストに関するものです。私はそれらを置き換える必要があります。 –