<?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>boolean.co.nz &#187; MySQL</title>
	<atom:link href="http://boolean.co.nz/blog/category/development/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://boolean.co.nz/blog</link>
	<description>Where there is more to logic than TRUE or FALSE</description>
	<lastBuildDate>Sun, 29 Jan 2012 03:16:33 +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>Find the size of all databases on a server</title>
		<link>http://boolean.co.nz/blog/find-the-size-of-all-databases-on-a-server/604/</link>
		<comments>http://boolean.co.nz/blog/find-the-size-of-all-databases-on-a-server/604/#comments</comments>
		<pubDate>Tue, 30 Aug 2011 11:49:33 +0000</pubDate>
		<dc:creator>Boolean</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://boolean.co.nz/blog/?p=604</guid>
		<description><![CDATA[A quick way to create a view to show file size statistics across databases on a server. DROP VIEW IF EXISTS alldb; CREATE VIEW AllDatabases AS SELECT s.schema_name AS &#8216;Schema&#8217;, SUM (t.data_length) AS Data, SUM ( t.index_length ) AS Indexes, SUM (t.data_length) + SUM (t.index_length) AS &#8216;Mb Used&#8217;, IF (SUM(t.data_free)=0,&#8221;,SUM (t.data_free)) As &#8216;Mb Free&#8217;, IF [...]]]></description>
			<content:encoded><![CDATA[<table>
<tr>
<td>
<img src="http://boolean.co.nz/blog/wp-content/uploads/2009/04/mysql_logo.png" alt="" title="mysql_logo" width="204" height="106" class="aligncenter size-full wp-image-96" /></td>
</tr>
</table>
<p>A quick way to create a view to show file size statistics across databases on a server.</p>
<div class="codesnip-container" >DROP VIEW IF EXISTS alldb;<br />
CREATE VIEW AllDatabases AS<br />
SELECT<br />
s.schema_name AS &#8216;Schema&#8217;,<br />
SUM (t.data_length) AS Data,<br />
SUM ( t.index_length ) AS Indexes,<br />
SUM (t.data_length) + SUM (t.index_length) AS &#8216;Mb Used&#8217;,<br />
IF (SUM(t.data_free)=0,&#8221;,SUM (t.data_free)) As &#8216;Mb Free&#8217;,<br />
IF (SUM(t.data_free)=0,&#8221;, 100 * (SUM (t.data_length) + SUM (t.index_length)) / ((SUM (t.data_length)+SUM (t.index_length) + SUM (IFNULL(t.data_free,0))) ) ) AS &#8216;Pct Used&#8217;, COUNT (table_name) AS Tables<br />
FROM information_schema.schemata s<br />
LEFT JOIN information_schema.tables t ON s.schema_name = t.table_schema<br />
GROUP BY s.schema_name<br />
WITH ROLLUP</div>
]]></content:encoded>
			<wfw:commentRss>http://boolean.co.nz/blog/find-the-size-of-all-databases-on-a-server/604/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exporting a MySQL Database Schema as XML</title>
		<link>http://boolean.co.nz/blog/exporting-a-mysql-database-schema-as-xml/354/</link>
		<comments>http://boolean.co.nz/blog/exporting-a-mysql-database-schema-as-xml/354/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 10:10:12 +0000</pubDate>
		<dc:creator>Boolean</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://boolean.co.nz/blog/?p=354</guid>
		<description><![CDATA[Dumping a database schema is a an often required quick task. This script will read the schema from a MySQL database, and output it XML to describe it. &#60;?php // Define a few constants define(&#8220;DB_SERVER&#8221;, &#8220;localhost&#8221;); define(&#8220;DB_USER&#8221;, &#8220;root&#8221;); define(&#8220;DB_PASS&#8221;, &#8220;password&#8221;); define(&#8220;DB_NAME&#8221;, &#8220;example&#8221;); // Lets connect to the db $dbhandle = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(&#8220;Unable [...]]]></description>
			<content:encoded><![CDATA[<table>
<tr>
<td><img src="http://boolean.co.nz/blog/wp-content/uploads/2009/04/mysql_logo.png" alt="" title="mysql_logo" width="204" height="106" class="aligncenter size-full wp-image-96" /></td>
</tr>
</table>
<p>Dumping a database schema is a an often required quick task. This script will read the schema from a MySQL database, and output it XML to describe it.</p>
<div class="codesnip-container" >&lt;?php<br />
// Define a few constants<br />
define(&#8220;DB_SERVER&#8221;, &#8220;localhost&#8221;);<br />
define(&#8220;DB_USER&#8221;, &#8220;root&#8221;);<br />
define(&#8220;DB_PASS&#8221;, &#8220;password&#8221;);<br />
define(&#8220;DB_NAME&#8221;, &#8220;example&#8221;);</p>
<p>// Lets connect to the db<br />
$dbhandle = mysql_connect(DB_SERVER, DB_USER, DB_PASS)<br />
   or die(&#8220;Unable to connect to MySQL&#8221;); </p>
<p>// Choose a database to work with<br />
$selected = mysql_select_db(DB_NAME, $dbhandle)<br />
   or die(&#8220;Could not select examples&#8221;);<br />
// Return all of the available tables<br />
$result_tbl = mysql_query( &#8220;SHOW TABLES FROM &#8220;.DB_NAME, $dbhandle ); </p>
<p>$tables = array();<br />
while ($row = mysql_fetch_row($result_tbl)) {<br />
   $tables[] = $row[0];<br />
} </p>
<p>$output = &#8220;&lt;?xml version=\&#8221;1.0\&#8221; ?&gt;\n&#8221;;<br />
$output .= &#8220;&lt;schema&gt;&#8221;; </p>
<p>// Now iterate through each table and return the fields<br />
foreach ( $tables as $table ) {<br />
   $output .= &#8220;&lt;table name=\&#8221;$table\&#8221;&gt;&#8221;;<br />
   $result_fld = mysql_query( &#8220;SHOW FIELDS FROM &#8220;.$table, $dbhandle ); </p>
<p>   while( $row1 = mysql_fetch_row($result_fld) ) {<br />
      $output .= &#8220;&lt;field name=\&#8221;$row1[0]\&#8221; type=\&#8221;$row1[1]\&#8221;";<br />
      $output .= ($row1[3] == &#8220;PRI&#8221;) ? &#8221; primary_key=\&#8221;yes\&#8221; /&gt;&#8221; : &#8221; /&gt;&#8221;;<br />
   } </p>
<p>   $output .= &#8220;&lt;/table&gt;&#8221;;<br />
} </p>
<p>$output .= &#8220;&lt;/schema&gt;&#8221;; </p>
<p>// Notify the browser the type of file being dealt with<br />
header(&#8220;Content-type: text/xml&#8221;);<br />
// Display the XML to describe the schema<br />
echo $output; </p>
<p>// Dont forget to close the connection<br />
mysql_close($dbhandle);<br />
?&gt;</p></div>
<p>Remember, this is an example, and real world code should escape characters, follow best security practices etc. Proof of concept code here, &#8211; not a snippet to put live.</p>
]]></content:encoded>
			<wfw:commentRss>http://boolean.co.nz/blog/exporting-a-mysql-database-schema-as-xml/354/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scheduling mySQL backups</title>
		<link>http://boolean.co.nz/blog/scheduling-mysql-backups/316/</link>
		<comments>http://boolean.co.nz/blog/scheduling-mysql-backups/316/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 02:31:55 +0000</pubDate>
		<dc:creator>Boolean</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://boolean.co.nz/blog/?p=316</guid>
		<description><![CDATA[I&#8217;m sure there is no need to explain the virtues of regular backups. Below are three steps to peace of mind 1) Configure a backup directory on the server mkdir /var/lib/backupfolder cd /var/lib/backupfolder 2) Make a backup script vi mysqlbackup.sh #!/bin/sh # MySQL server username goes here USERNAME=&#8221;username&#8221; # MySQL server password goes here PASSWORD=&#8221;password&#8221; [...]]]></description>
			<content:encoded><![CDATA[<table>
<tr>
<td>
<img src="http://boolean.co.nz/blog/wp-content/uploads/2009/04/mysql_logo.png" alt="" title="mysql_logo" width="204" height="106" class="aligncenter size-full wp-image-96" />
</td>
</tr>
</table>
<p>I&#8217;m sure there is no need to explain the virtues of regular backups. Below are three steps to peace of mind</p>
<p>1) Configure a backup directory on the server</p>
<div class="codesnip-container" >mkdir /var/lib/backupfolder<br />
cd /var/lib/backupfolder</div>
<p>2) Make a backup script</p>
<div class="codesnip-container" >vi mysqlbackup.sh</div>
<div class="codesnip-container" >#!/bin/sh<br />
# MySQL server username goes here<br />
USERNAME=&#8221;username&#8221;<br />
# MySQL server password goes here<br />
PASSWORD=&#8221;password&#8221;<br />
# List of DBNAMES for Backup<br />
DBNAME=&#8221;dbname&#8221;<br />
#date timestamp used in the log<br />
DATE=`/bin/date +%Y-%m-%d_%Hh%Mm`<br />
# format the output file<br />
OUTDIR=&#8221;/var/lib/backupfolder/&#8221;<br />
OUTFILE=&#8221;ip_bindass&#8221;.$DATE.&#8221;sql.gz&#8221;<br />
#working directory<br />
DIR=&#8221;/var/lib/backupfolder/&#8221;<br />
#cd $DIR<br />
# Do the MySQL Backup<br />
/usr/bin/mysqldump &#8211;database $DBNAME &#8211;opt &#8211;single-transaction -u$USERNAME -p$PASSWORD | /usr/bin/gzip -9 > $OUTDIR$OUTFILE</div>
<p>Dont forget to change permissions</p>
<div class="codesnip-container" >chmod +x mysqlbackup.sh</div>
<p>3) Schedule the backup in crontab</p>
<div class="codesnip-container" >crontab -e</div>
<p>Add something along the lines of:</p>
<div class="codesnip-container" >30 23 * * * /var/lib/backupfolder/mysqlbackup.sh</div>
<p>To backup at 11:30PM every day. Just as important as to backup up is to test restores; &#8211; it&#8217;s no good having backups and feeling at ease if you cant recover in the event of catastrophe!</p>
]]></content:encoded>
			<wfw:commentRss>http://boolean.co.nz/blog/scheduling-mysql-backups/316/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Help keep the internet free</title>
		<link>http://boolean.co.nz/blog/help-keep-the-internet-free/247/</link>
		<comments>http://boolean.co.nz/blog/help-keep-the-internet-free/247/#comments</comments>
		<pubDate>Fri, 01 Jan 2010 21:44:48 +0000</pubDate>
		<dc:creator>Boolean</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://boolean.co.nz/blog/?p=247</guid>
		<description><![CDATA[A big part of the Internet is built on LAMP (Linux, Apache, MySQL and PHP/Perl/Python). Now Oracle is trying to buy Sun, which owns MySQL. It&#8217;s not in the Internet users interest that one key piece of the net would be owned by an entity that has more to gain by severely limiting and in [...]]]></description>
			<content:encoded><![CDATA[<table>
<tr>
<td>
<img src="http://boolean.co.nz/blog/wp-content/uploads/2009/04/mysql_logo.png" alt="mysql_logo" title="mysql_logo" width="204" height="106" class="aligncenter size-full wp-image-96" />
</td>
</tr>
</table>
<p>A big part of the Internet is built on LAMP (Linux, Apache, MySQL and PHP/Perl/Python). Now Oracle is trying to buy Sun, which owns MySQL.</p>
<p>It&#8217;s not in the Internet users interest that one key piece of the net would be owned by an entity that has more to gain by severely limiting and in the long run even killing it as an open source product than by keeping it alive. If Oracle were allowed to acquire MySQL, we would be looking at less competition among databases, which will mean higher license and support prices. In the end it&#8217;s always the consumers and the small businesses that have to pay the bills, in this case to Oracle.</p>
<p>Read on <a href="http://monty-says.blogspot.com/2009/12/help-keep-internet-free.html">here</a> at Monty&#8217;s blog (creator of MySQL) or head <a href="http://www.helpmysql.org">here</a> for the official website.</p>
]]></content:encoded>
			<wfw:commentRss>http://boolean.co.nz/blog/help-keep-the-internet-free/247/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using LIMIT when querying a single row</title>
		<link>http://boolean.co.nz/blog/using-limit-when-querying-a-single-row/239/</link>
		<comments>http://boolean.co.nz/blog/using-limit-when-querying-a-single-row/239/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 13:49:07 +0000</pubDate>
		<dc:creator>Boolean</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://boolean.co.nz/blog/?p=239</guid>
		<description><![CDATA[Sometimes you know you are only looking for one row when you are querying your tables. It may be that you are fetching a unique record or checking the existence of any number of records that satisfy your WHERE clause. Adding LIMIT 1 to your query can increase performance in this situation; &#8211; the database [...]]]></description>
			<content:encoded><![CDATA[<table>
<tr>
<td>
<img src="http://boolean.co.nz/blog/wp-content/uploads/2009/04/mysql_logo.png" alt="mysql_logo" title="mysql_logo" width="204" height="106" class="aligncenter size-full wp-image-96" />
</td>
</tr>
</table>
<p>Sometimes you know you are only looking for one row when you are querying your tables.  It may be that you are fetching a unique record or checking the existence of any number of records that satisfy your WHERE clause.</p>
<p>Adding LIMIT 1 to your query can increase performance in this situation; &#8211; the database engine will stop scanning for records after it finds just one, instead of going through the whole table or index.</p>
<p>For example:</p>
<div class="codesnip-container" >$record = mysql_query(&#8220;SELECT username FROM user WHERE country = &#8216;UK&#8217;&#8221;);<br />
if (mysql_num_rows($record) > 0) {<br />
// &#8230;<br />
}</div>
<p>Can become:</p>
<div class="codesnip-container" >$record = mysql_query(&#8220;SELECT username FROM user WHERE country = &#8216;UK&#8217; LIMIT 1&#8243;);<br />
if (mysql_num_rows($record) > 0) {<br />
 // &#8230;<br />
}</div>
]]></content:encoded>
			<wfw:commentRss>http://boolean.co.nz/blog/using-limit-when-querying-a-single-row/239/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maxlength for MySQL TEXT field types</title>
		<link>http://boolean.co.nz/blog/max-length-for-mysql-text-field-types/135/</link>
		<comments>http://boolean.co.nz/blog/max-length-for-mysql-text-field-types/135/#comments</comments>
		<pubDate>Fri, 08 May 2009 08:33:56 +0000</pubDate>
		<dc:creator>Boolean</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://boolean.co.nz/blog/?p=135</guid>
		<description><![CDATA[MySQL supports four TEXT field types (TINYTEXT, TEXT, MEDIUMTEXT and LONGTEXT) MyISAM tables in MySQL have a maximum row size 65,535 bytes and all the data in a row must fit within that limit. Luckily however TEXT field types are stored outside of the table itself and thus only contribute 9 &#8211; 12 bytes towards [...]]]></description>
			<content:encoded><![CDATA[<table>
<tr>
<td>
<img src="http://boolean.co.nz/blog/wp-content/uploads/2009/04/mysql_logo.png" alt="mysql_logo" title="mysql_logo" width="204" height="106" class="aligncenter size-full wp-image-96" />
</td>
</tr>
</table>
<p><strong>MySQL supports four TEXT field types (TINYTEXT, TEXT, MEDIUMTEXT and LONGTEXT)</strong></p>
<p>MyISAM tables in MySQL have a maximum row size 65,535 bytes and <em>all</em> the data in a row must fit within that limit.</p>
<p>Luckily however TEXT field types are stored outside of the table itself and thus only contribute 9 &#8211; 12 bytes towards that limit.</p>
<p>Further reading is <a href="http://dev.mysql.com/doc/refman/5.1/en/storage-requirements.html">here.</a></p>
<p>Because TEXT data types are able to store so much more data than VARCHAR and CHAR field types it makes sense to use them when storing web pages or similar content in the database.</p>
<p>The maximum amount of data that can be stored for each data type is approximately:</p>
<p>TINYTEXT 256 bytes<br />
TEXT 65,535 bytes ~64kb<br />
MEDIUMTEXT  16,777,215 bytes ~16MB<br />
LONGTEXT 4,294,967,295 bytes ~4GB </p>
<p>So most of the time TEXT will suffice, but if you are scratch building a CMS it might be an idea to think about MEDIUMTEXT</p>
<p><em>Update: (20/05/201) &#8211; I see a lot of hits on this page, so I thought I&#8217;d spell out the information here for the terms you seem to be searching for&#8230;</em></p>
<p><strong>TINYTEXT</strong> is a string data type that can store up to to 255 characters. </p>
<p><strong>TEXT</strong> is a string data type that can store up to 65,535 characters. TEXT is commonly used for storing blocks of text used for brief articles. </p>
<p><strong>LONGTEXT</strong> is a string data type with a maximum length of 4,294,967,295 characters. Use LONGTEXT if you need to store large blocks of text, such as a chapter of a book.  </p>
]]></content:encoded>
			<wfw:commentRss>http://boolean.co.nz/blog/max-length-for-mysql-text-field-types/135/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL date_format</title>
		<link>http://boolean.co.nz/blog/mysql-date_format/118/</link>
		<comments>http://boolean.co.nz/blog/mysql-date_format/118/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 16:48:56 +0000</pubDate>
		<dc:creator>Boolean</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://boolean.co.nz/blog/?p=118</guid>
		<description><![CDATA[I always seem to be digging around to find MySQL&#8217;s date formatting syntax, so here is a couple of common conversions&#8230; select date_format(date, &#8216;%d %M %Y&#8217;) as new_date from tablename where date is the name of your date field, and new_date is the variable name which you can use to reference the value. date_format String [...]]]></description>
			<content:encoded><![CDATA[<table></tr>
<td>
<img src="http://boolean.co.nz/blog/wp-content/uploads/2009/04/mysql_logo.png" alt="mysql_logo" title="mysql_logo" width="204" height="106" class="aligncenter size-full wp-image-96" />
</td>
</tr>
</table>
<p>I always seem to be digging around to find MySQL&#8217;s date formatting syntax, so here is a couple of common conversions&#8230;</p>
<div class="codesnip-container" >select date_format(date, &#8216;%d %M %Y&#8217;) as new_date from tablename</div>
<p>where <strong>date</strong> is the name of your date field, and <strong>new_date </strong>is the variable name which you can use to reference the value.</p>
<table>
<tr>
<td><strong>date_format String</strong></td>
<td><strong>Example</strong></td>
</tr>
<tr>
<td>&#8216;%e/%c/%Y&#8217;</td>
<td> 25/4/2009 </td>
</tr>
<tr>
<td>&#8216;%c/%e/%Y&#8217;</td>
<td>4/25/2009</td>
</tr>
<tr>
<td>&#8216;%d/%m/%Y&#8217;</td>
<td>25/04/2009</td>
</tr>
<tr>
<td>&#8216;%m/%d/%Y&#8217;</td>
<td>04/25/2009</td>
</tr>
<tr>
<td>&#8216;%a %D %b %Y&#8217;</td>
<td>Fri 25th Apr 2009</td>
</tr>
</table>
<p>A more complete list of specifiers is available <a href="http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://boolean.co.nz/blog/mysql-date_format/118/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>MySQL PHP &amp; UTF-8</title>
		<link>http://boolean.co.nz/blog/mysql-php-utf-8/95/</link>
		<comments>http://boolean.co.nz/blog/mysql-php-utf-8/95/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 09:32:43 +0000</pubDate>
		<dc:creator>Boolean</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://boolean.co.nz/blog/?p=95</guid>
		<description><![CDATA[Its been a while but I was getting characters like Â£ instead of £ (for example) and it took a quick refresher to realise what was going on. A fantastic background reading is available here. So what is going on? UTF-8 uses one or more 8-bit bytes to store a single character, whereas ASCII for [...]]]></description>
			<content:encoded><![CDATA[<table>
<tr>
<td>
<img src="http://boolean.co.nz/blog/wp-content/uploads/2009/04/mysql_logo.png" alt="mysql_logo" title="mysql_logo" width="204" height="106" class="aligncenter size-full wp-image-96" />
</td>
</tr>
</table>
<p>Its been a while but I was getting characters like Â£ instead of £ (for example) and it took a quick refresher to realise what was going on.  A fantastic background reading is available <a href="http://www.joelonsoftware.com/articles/Unicode.html">here.</a></p>
<p><strong>So what is going on?</strong><br />
UTF-8 uses one or more 8-bit bytes to store a single character, whereas ASCII for example uses only one byte per character. This makes it more space-efficient than UTF-16 or UTF-32 as the majority of  characters can be single byte encoded but with the added benefit that you can store <em>any</em> character, &#8211; should you need to. It uses the most significant bits of each byte as continuation bits (to signify that the following byte(s) form part of the same character) and it this is the reason that improperly displayed UTF-8 results in weird characters.</p>
<p><strong>OK, enough history, &#8211; lets just fix it!</strong><br />
The solution is to make sure you set the character set correctly with a Content-type header.</p>
<div class="codesnip-container" >Content-type: text/html; charset=utf-8<br />
&lt;meta http-equiv=&#8221;content-type&#8221; content=&#8221;text/html; charset=utf-8&#8243;&gt;</div>
<p>So now that we are ready to store UTF-8 data we store some rows of data and output to the browser looks fine. However, is it really?</p>
<p>MySQL needs to know the character set of the data sent to it in your queries. The default connection character set is ISO 8859-1 (latin1) &#8212; treating all your supplied data as if it was ISO 8859-1, which is then automatically converted to the underlying character set of the column (UTF-8 in our case). Taking our earlier example this means that the two-byte pound sign is perceived as two ISO 8859-1 characters: Â and £. MySQL will then encode these characters separately as UTF-8 requiring 2 bytes each &#8211; double encoding as we use 4 bytes to store a single pound character! When selecting data from the table, the reverse occurs when MySQL converts the UTF-8 back into ISO 8859-1 and the browser (correctly) interprets the two bytes as a pound sign. The problem here is that while everything looks correct it is needlessly using extra storage space and CPU cycles in the conversion.</p>
<p>It&#8217;s very simple to change the connection character set and avoid these overheads. The query &#8220;SET NAMES &#8216;utf8&#8242;&#8221; instructs MySQL to treat incoming data as UTF-8, which can be directly inserted into columns with a matching charset. In practice you may issue this query after initialising your connection to the MySQL server so all future communication will be in UTF-8.</p>
<p>One major problem occurs when you have a table full of double-encoded UTF-8 text because it was inserted before you knew about changing the connection character set. If you then add in the extra &#8220;SET NAMES&#8221; query and output the retrieved text to a browser you will notice all the extra characters have crept in.</p>
<p>You may be tempted to be selective about where you use SET NAMES &#8216;utf8&#8242; however it is fairly simple to modify all of the data in place and end up with clean tables. Here is some pseudocode that pulls valid UTF-8 from the table and reinserts it after switching the connection charset:</p>
<div class="codesnip-container" >SET NAMES &#8216;latin1&#8242;;<br />
SELECT id, col1, col2, col3, col4 FROM table;<br />
SET NAMES &#8216;utf8&#8242;;<br />
for each row<br />
	INSERT INTO table (col1, col2, col3, col4) VALUES (, , , ) WHERE id = ;</div>
]]></content:encoded>
			<wfw:commentRss>http://boolean.co.nz/blog/mysql-php-utf-8/95/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

