<?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>Ersin Acar &#187; wrong</title>
	<atom:link href="http://ersinacar.com/tag/wrong/feed" rel="self" type="application/rss+xml" />
	<link>http://ersinacar.com</link>
	<description>PHP and Web Technologies Freak</description>
	<lastBuildDate>Fri, 06 Jan 2012 14:19:52 +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>Change/Fix Wrong MySQL Encoding</title>
		<link>http://ersinacar.com/changefix-wrong-mysql-encoding_89.html</link>
		<comments>http://ersinacar.com/changefix-wrong-mysql-encoding_89.html#comments</comments>
		<pubDate>Fri, 24 Apr 2009 09:37:00 +0000</pubDate>
		<dc:creator>Ersin Acar</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[charset]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[wrong]]></category>

		<guid isPermaLink="false">http://ersinacar.com/?p=89</guid>
		<description><![CDATA[Sometimes, especially when moving data from one server to another, you might find that you have your MySQL database incorrectly encoded. Problem with this first show itself if you have the database and encoded in one charset set to display your website in another. If this is the case then you will find strange characters [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, especially when moving data from one server to another, you might find that you have your MySQL database incorrectly encoded. Problem with this first show itself if you have the database and encoded in one charset set to display your website in another. If this is the case then you will find strange characters appearing in your text, especially when using punctuation marks. If you are Unwilling or unable to change the character encoding on the site then you need to change how the data is encoded in the database.</p>
<p>The most common sort of thing you might want to do is change from iso-8859-1 (or windows-1252) to UTF-8. This can be done in one of two ways.</p>
<p>The first way is to simply alter the table so that the column contains a different charset.</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">ALTER</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #990099; font-weight: bold;">table</span> <span style="color: #990099; font-weight: bold;">MODIFY</span> col1 <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">50</span><span style="color: #FF00FF;">&#41;</span> CHARACTER <span style="color: #990099; font-weight: bold;">SET</span> <span style="color: #008000;">'utf8'</span><span style="color: #000033;">;</span></pre></div></div>

<p>However, if your database has already been set up and your data has already been inserted in the wrong format then you can also update the data in the column using the CONVERT command. The following snippet turns our latin1 data into uncoded binary data and then into utf8.</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">UPDATE</span> <span style="color: #990099; font-weight: bold;">table</span> <span style="color: #990099; font-weight: bold;">SET</span> col1<span style="color: #CC0099;">=</span><span style="color: #990099; font-weight: bold;">CONVERT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">CONVERT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">CONVERT</span><span style="color: #FF00FF;">&#40;</span>col1 <span style="color: #990099; font-weight: bold;">USING</span> <span style="color: #008000;">'latin1'</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">USING</span> <span style="color: #990099; font-weight: bold;">BINARY</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">USING</span> <span style="color: #008000;">'utf8'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></div></div>

<p>You should also make sure that the connection to the database is done through a specific character set. This is done by using the<br />
<span id="more-89"></span></p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SET</span> NAMES command <span style="color: #CC0099; font-weight: bold;">and</span> the <span style="color: #990099; font-weight: bold;">SET</span> CHARACTER <span style="color: #990099; font-weight: bold;">SET</span>.
&nbsp;
<span style="color: #990099; font-weight: bold;">SET</span> NAMES <span style="color: #008000;">'charset<span style="color: #008080; font-weight: bold;">_</span>name'</span>
<span style="color: #990099; font-weight: bold;">SET</span> CHARACTER <span style="color: #990099; font-weight: bold;">SET</span> <span style="color: #008000;">'charset<span style="color: #008080; font-weight: bold;">_</span>name'</span><span style="color: #000033;">;</span></pre></div></div>

<p>These two commands basically set some values in your MySQL database, for more information on what is set look at the Connection Character Sets and Collations page on the MySQL website. This ensures that the data we get back from the database is also in the correct charset.</p>
<p>For a full list of the different character sets available in MySQL just run the command:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SHOW</span> CHARACTER <span style="color: #990099; font-weight: bold;">SET</span><span style="color: #000033;">;</span></pre></div></div>

<p>This will display a table with the columns Charset, Description, Default collation and Maxlen. Each charset is associated with a collation. A collation is a set of rules for comparing characters in a charset, so it is important that you get this right if you want the database to work. The full list of collations can be viewed using the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SHOW</span> <span style="color: #000099;">COLLATION</span><span style="color: #000033;">;</span></pre></div></div>

<p>You can even use a LIKE statement to refine the collation data into the information you are looking for.</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SHOW</span> <span style="color: #000099;">COLLATION</span> <span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #FF9900; font-weight: bold;">Charset</span> <span style="color: #CC0099; font-weight: bold;">LIKE</span> <span style="color: #008000;">'<span style="color: #008080; font-weight: bold;">%</span>utf<span style="color: #008080; font-weight: bold;">%</span>'</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ersinacar.com/changefix-wrong-mysql-encoding_89.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

