<?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 &#187; pdo</title>
	<atom:link href="http://www.galengrover.com/tag/pdo/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.galengrover.com</link>
	<description>web developer, mariokart expert, professional sitter</description>
	<lastBuildDate>Wed, 21 Dec 2011 20:02:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Setting mysql connection charset with PDO</title>
		<link>http://www.galengrover.com/php/setting-mysql-connection-charset-with-pdo/</link>
		<comments>http://www.galengrover.com/php/setting-mysql-connection-charset-with-pdo/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 01:11:12 +0000</pubDate>
		<dc:creator>Galen</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[charset]]></category>
		<category><![CDATA[pdo]]></category>

		<guid isPermaLink="false">http://www.galengrover.com/?p=32</guid>
		<description><![CDATA[Recently while creating a store locator for a Chinese website 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 [...]]]></description>
			<content:encoded><![CDATA[<p>Recently while creating a store locator for a Chinese website 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"><div 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></div></div>

<blockquote><p>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.) </p></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;">=&amp;</span>gt<span style="color: #339933;">;</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/php/setting-mysql-connection-charset-with-pdo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Returning an instance of a class from PDO</title>
		<link>http://www.galengrover.com/php/returning-an-instance-of-a-class-from-pdo/</link>
		<comments>http://www.galengrover.com/php/returning-an-instance-of-a-class-from-pdo/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 03:38:37 +0000</pubDate>
		<dc:creator>Galen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[pdo]]></category>

		<guid isPermaLink="false">http://www.galengrover.com/?p=89</guid>
		<description><![CDATA[PDO can create and return an object(s) of a specific class. It can be a little tricky getting it to work though&#8230; With fetchAll() you can pass ( PDO:FETCH_CLASS, &#8216;class_name&#8217; ) directly $sql = sprintf&#40; 'select * from %s where prod_id=:prod_id', TABLE &#41;; $findProducts = $this-&#62;db-&#62;prepare&#40; $sql &#41;; $findProduct-&#62;execute&#40;&#41;; $products = $findProducts-&#62;fetchAll&#40; PDO::FETCH_CLASS, 'Product' &#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>PDO can create and return an object(s) of a specific class. It can be a little tricky getting it to work though&#8230;</p>
<p>With <a href="http://www.php.net/manual/en/pdostatement.fetchall.php">fetchAll()</a> you can pass ( PDO:FETCH_CLASS, &#8216;class_name&#8217; ) directly</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'select * from %s where prod_id=:prod_id'</span><span style="color: #339933;">,</span> TABLE <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$findProducts</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$sql</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$findProduct</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$products</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$findProducts</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchAll</span><span style="color: #009900;">&#40;</span> PDO<span style="color: #339933;">::</span><span style="color: #004000;">FETCH_CLASS</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Product'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>But with <a href="http://www.php.net/manual/en/pdostatement.fetch.php">fetch()</a> you must <em>initiate</em> it with <a href="http://www.php.net/manual/en/pdostatement.setfetchmode.php">setFetchMode()</a></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'select * from %s where prod_id=:prod_id'</span><span style="color: #339933;">,</span> TABLE <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$findProduct</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$sql</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$findProduct</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFetchMode</span><span style="color: #009900;">&#40;</span> PDO<span style="color: #339933;">::</span><span style="color: #004000;">FETCH_CLASS</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Product'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$findProduct</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$product</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$findProduct</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch</span><span style="color: #009900;">&#40;</span> PDO<span style="color: #339933;">::</span><span style="color: #004000;">FETCH_CLASS</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.galengrover.com/php/returning-an-instance-of-a-class-from-pdo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

