<?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>Galen Grover - Connecticut Web Developer &#187; MySQL</title>
	<atom:link href="http://www.galengrover.com/cat/web-development/mysql-web-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.galengrover.com</link>
	<description>Sometimes I write things</description>
	<lastBuildDate>Fri, 03 Sep 2010 18:12:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MySQL custom sort order</title>
		<link>http://www.galengrover.com/web-development/mysql-custom-sort-order/</link>
		<comments>http://www.galengrover.com/web-development/mysql-custom-sort-order/#comments</comments>
		<pubDate>Wed, 09 Sep 2009 18:33:18 +0000</pubDate>
		<dc:creator>Galen</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.galengrover.com/?p=472</guid>
		<description><![CDATA[Here is a way to return results from a table in a custom order


select id from products where id in &#40;4,5,6,7,8,9&#41; order by field&#40;id,4,7,6,5,9,8&#41;


]]></description>
			<content:encoded><![CDATA[<p>Here is a way to return results from a table in a custom order</p>


<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">select</span> id <span style="color: #990099; font-weight: bold;">from</span> products <span style="color: #990099; font-weight: bold;">where</span> id <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">4</span><span style="color: #000033;">,</span><span style="color: #008080;">5</span><span style="color: #000033;">,</span><span style="color: #008080;">6</span><span style="color: #000033;">,</span><span style="color: #008080;">7</span><span style="color: #000033;">,</span><span style="color: #008080;">8</span><span style="color: #000033;">,</span><span style="color: #008080;">9</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">order by</span> <span style="color: #000099;">field</span><span style="color: #FF00FF;">&#40;</span>id<span style="color: #000033;">,</span><span style="color: #008080;">4</span><span style="color: #000033;">,</span><span style="color: #008080;">7</span><span style="color: #000033;">,</span><span style="color: #008080;">6</span><span style="color: #000033;">,</span><span style="color: #008080;">5</span><span style="color: #000033;">,</span><span style="color: #008080;">9</span><span style="color: #000033;">,</span><span style="color: #008080;">8</span><span style="color: #FF00FF;">&#41;</span></pre></div></div>


]]></content:encoded>
			<wfw:commentRss>http://www.galengrover.com/web-development/mysql-custom-sort-order/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting mysql connection charset with PDO</title>
		<link>http://www.galengrover.com/web-development/setting-the-connection-charset-with-php-mysql/</link>
		<comments>http://www.galengrover.com/web-development/setting-the-connection-charset-with-php-mysql/#comments</comments>
		<pubDate>Tue, 12 May 2009 23:41:07 +0000</pubDate>
		<dc:creator>Galen</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Charsets]]></category>
		<category><![CDATA[Mysql]]></category>
		<category><![CDATA[PDO]]></category>

		<guid isPermaLink="false">http://www.galengrover.com/?p=3</guid>
		<description><![CDATA[Recently while creating a store locator for a Chinese company I ran into a problem. Some Chinese characters were being displayed correctly and some weren&#8217;t. First thing I did was check the charset of the HTTP headers and the database&#8230; both were set correctly to utf-8.  I figured it had to be a database [...]]]></description>
			<content:encoded><![CDATA[<p>Recently while creating a store locator for a Chinese company I ran into a problem. Some Chinese characters were being displayed correctly and some weren&#8217;t. First thing I did was check the charset of the HTTP headers and the database&#8230; both were set correctly to utf-8.  I figured it had to be a database issue so I began googling and found <a href="http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html">this</a>.</p>

<p>Apparently you have to set the character set of the connection as well via:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SET</span> NAMES <span style="color: #008000;">'utf-8'</span></pre></td></tr></table></div>




<blockquote>SET NAMES indicates what character set the client will use to send SQL statements to the server. Thus, SET NAMES &#8216;cp1251&#8242; tells the server “future incoming messages from this client are in character set cp1251.” It also specifies the character set that the server should use for sending results back to the client. (For example, it indicates what character set to use for column values if you use a SELECT statement.) </blockquote>

<p>Great!</p>

<p>The only issue remaining was the fact that I didn&#8217;t want to have to run that query on every page.</p>

<p>I use <a href="http://us3.php.net/pdo">PDO</a>, and I found out you can use the driver_options argument of PDO to run an initial command.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dsn</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'mysql:dbname=%s;host=%s'</span><span style="color: #339933;">,</span> DB_NAME<span style="color: #339933;">,</span> DB_HOST <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$driver_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> PDO<span style="color: #339933;">::</span><span style="color: #004000;">MYSQL_ATTR_INIT_COMMAND</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'SET NAMES utf-8'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
try <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$dbc</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> pdo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dsn</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pw</span><span style="color: #339933;">,</span> <span style="color: #000088;">$driver_options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
catch <span style="color: #009900;">&#40;</span>PDOException <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Handle exception</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>




<p><strong>Good reads about character sets</strong></p>
<ul>
	<li><a href="http://www.joelonsoftware.com/articles/Unicode.html">The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets</a></li>
	<li><a href="http://www.phpwact.org/php/i18n/charsets">Character Sets / Character Encoding Issues</a></li>
	<li><a href="http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html">Mysql &#8211; Connection Character Sets and Collations</a></li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://www.galengrover.com/web-development/setting-the-connection-charset-with-php-mysql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
