<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ROAE-Blog &#187; utf8</title>
	<atom:link href="http://blog.root-of-all-evil.com/tag/utf8/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.root-of-all-evil.com</link>
	<description>Studium, Codeing und Gedachtes</description>
	<lastBuildDate>Mon, 30 Jan 2012 22:58:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Den Inhalt einer mysql Datenbank in utf8 umwandeln</title>
		<link>http://blog.root-of-all-evil.com/2010/03/den-inhalt-einer-mysql-datenbank-in-utf8-umwandeln/</link>
		<comments>http://blog.root-of-all-evil.com/2010/03/den-inhalt-einer-mysql-datenbank-in-utf8-umwandeln/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 09:08:08 +0000</pubDate>
		<dc:creator>Philipp</dc:creator>
				<category><![CDATA[Codeschnipsel]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[convert]]></category>
		<category><![CDATA[converter]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[skript]]></category>
		<category><![CDATA[umwandeln]]></category>
		<category><![CDATA[utf8]]></category>

		<guid isPermaLink="false">http://blog.root-of-all-evil.com/?p=552</guid>
		<description><![CDATA[Es kann schon einiges an Kopfzerbrechen verursachen, wenn beispielsweise ein Wordpress-Update nur noch kryptische Zeichen statt Umlaute darstellt. Meist liegt das Problem am in der Datenbank verwendetem Zeichensatz. Daten, die beispielsweise in einem latin1 Zeichensatz formatiert sind und in einer utf8-Tabelle abgespeichert werden, werden dadurch beim Auslesen meist unbrauchbar.<p class="read-more"><a href="http://blog.root-of-all-evil.com/2010/03/den-inhalt-einer-mysql-datenbank-in-utf8-umwandeln/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://blog.root-of-all-evil.com/2010/03/den-inhalt-einer-mysql-datenbank-in-utf8-umwandeln/" title="Den Inhalt einer mysql Datenbank in utf8 umwandeln"></a><p>Es kann schon einiges an Kopfzerbrechen verursachen, wenn beispielsweise ein WordPress-Update nur noch kryptische Zeichen statt Umlaute darstellt. Meist liegt das Problem am in der Datenbank verwendetem Zeichensatz. Daten, die beispielsweise in einem latin1 Zeichensatz formatiert sind und in einer utf8-Tabelle abgespeichert werden, werden dadurch beim Auslesen meist unbrauchbar.</p>
<p>Das PHP-Skript hier hilft bei der Umwandlung des <em>Inhalts </em>der Datenbanktabellen in die utf8 Zeichenkodierung. Das Skript wandelt zunächst alle Tabellendaten in Binärdaten um, ändert dann die Tabellencodierung und wandelt schließlich die Tabellendaten wieder in das ursprüngliche Format um. Dadurch werden zum Beispiel Umlaute in ihre utf8-Entsprechungen verwandelt.</p>
<p><span id="more-552"></span></p>
<h3>Das ist zu tun</h3>
<ol>
<li>Zunächst alle Tabellen im Originalformat belassen</li>
<li><strong>Eine SICHERHEITSKOPIE DER DATENBANK ANLEGEN</strong></li>
<li>Das Skript in einem Texteditor öffnen und die Datenbankparameter anpassen</li>
<li>Das Skript auf den Webspace hochladen und im Browser öffnen</li>
<li>Das Skript vom Webspace löschen</li>
</ol>
<h3>Wie funktioniert das Skript? Kann ich das auch ohne Skript machen?</h3>
<p>Der Trick ist ganz einfach: Wandel ich den Zeichenkodierung einer Tabellenspalte mit folgendem mysql-Befehl in Binärdaten um &#8230;</p>
<pre title="code" class="sql">ALTER TABLE Tabellenname CONVERT TO CHARACTER SET binary</pre>
<p>&#8230; änder dann mit diesem Befehl hier die Zeichenkodierung der Tabelle auf utf8 &#8230;</p>
<pre title="code" class="sql">ALTER TABLE Tabellenname CONVERT TO CHARACTER SET utf8</pre>
<p>&#8230; muss ich im nächsten Schritt die Tabellenspalte wieder auf den ursprünglichen Typ zurücksetzen:</p>
<pre title="code" class="sql">ALTER TABLE Tabellenname MODIFY Spaltentyp Spaltenoptionen</pre>
<p>(Wobei Spaltentyp der ursprüngliche Spaltentyp wie Text, Varchar, Enum etc. ist und die Spaltenoptionen die Optionen wie zB. NOT NULL oder DEFAULT sind.)</p>
<h3>Das Skript</h3>
<pre class="php" title="code">&lt;?php

$DB_HOST = 'localhost'; // Enter your Database Host
$DB_USER = 'username'; // Enter your Database Username
$DB_PASSWORD = 'password'; // Enter your Database Password
$DB_DATABASE = 'database'; // Enter your Database Name

$tables = array();
$tables_with_fields = array();

$link_id = mysql_connect($DB_HOST, $DB_USER, $DB_PASSWORD) or die('Error establishing a database connection');
echo 'Connected' ."\n";
mysql_select_db($DB_DATABASE, $link_id);
echo 'Selected database' ."\n";

// Gathering information about tables and all the text/string fields that can be affected
// during the conversion to UTF-8.
echo 'Getting tables:' ."\n";
$resource = mysql_query("SHOW TABLES", $link_id);
while ( $result = mysql_fetch_row($resource) ) {
	$tables[] = $result[0];
	echo ' - ' . $result[0] ."\n";
}

if ( !empty($tables) ) {
    echo 'Starting process' ."\n";
    foreach ( (array) $tables as $table ) {
        echo 'Working on table "' . $table . '"';
        $resource = mysql_query("EXPLAIN $table", $link_id);
        while ( $result = mysql_fetch_assoc($resource) ) {
            if ( preg_match('/(char)|(text)|(enum)|(set)/', $result['Type']) )
                $tables_with_fields[$table][$result['Field']] = $result['Type'] . " " . ( "YES" == $result['Null'] ? "" : "NOT " ) . "NULL " .  ( !is_null($result['Default']) ? "DEFAULT '". $result['Default'] ."'" : "" );
                echo '.';
        }
        echo "\n";
    }

    // Change all text/string fields of the tables to their corresponding binary text/string representations.
    echo 'Altering tables to binary character set';
    foreach ( (array) $tables as $table ) {
        mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET binary", $link_id);
        echo '.';
    }
    echo "\n";

    // Change database and tables to UTF-8 Character set.
    echo 'Altering tables to utf8 character set';
    mysql_query("ALTER DATABASE " . $DB_DATABASE . " CHARACTER SET utf8", $link_id);
    foreach ( (array) $tables as $table ) {
        mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET utf8", $link_id);
        echo '.';
    }
    echo "\n";

    // Return all binary text/string fields previously changed to their original representations.
    echo 'Altering binary text/string fields to original representation';
    foreach ( (array) $tables_with_fields as $table =&gt; $fields ) {
        foreach ( (array) $fields as $field_type =&gt; $field_options ) {
            mysql_query("ALTER TABLE $table MODIFY $field_type $field_options", $link_id);
        }
        echo '.';
    }
    echo "\n";

    // Optimize tables and finally close the mysql link.
    echo 'Optimizing tables' . "\n";
    foreach ( (array) $tables as $table )
        mysql_query("OPTIMIZE TABLE $table", $link_id);
    mysql_close($link_id);
    echo 'DONE';
} else {
    die('There are no tables?');
}
?&gt;</pre>
<p>Das Skript basiert auf dem mitlerweile nicht mehr weiter entwickeltem <a href="http://wordpress.org/extend/plugins/utf-8-database-converter/" target="_blank">WordPress UTF-8 Database Converter</a> und lässt sich in der obigen Form <a href="http://blog.root-of-all-evil.com/wp-content/uploads/2010/03/utf8-converter.zip">hier herunterladen</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.root-of-all-evil.com/2010/03/den-inhalt-einer-mysql-datenbank-in-utf8-umwandeln/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

