<?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; PHP</title>
	<atom:link href="http://www.galengrover.com/cat/web-development/php-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>Making wordpress parent navigation items unclickable with jquery</title>
		<link>http://www.galengrover.com/web-development/making-wordpress-parent-navigation-items-unclickable-with-jquery/</link>
		<comments>http://www.galengrover.com/web-development/making-wordpress-parent-navigation-items-unclickable-with-jquery/#comments</comments>
		<pubDate>Sun, 18 Jul 2010 18:15:51 +0000</pubDate>
		<dc:creator>Galen</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.galengrover.com/?p=667</guid>
		<description><![CDATA[I&#8217;m developing a new site that has a navigation that is built dynamically via wordpress&#8217; wp_list_pages function


&#60;?php  wp_list_pages&#40;'sort_column=menu_order&#38;title_li=&#38;exclude=6'&#41;; ?&#62;




Navigation items that have submenus (css dropdowns) are still clickable, but for this website they have no content. I wanted a way to make those items unclickable. I was unable to find any way with wordpress&#8217; [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m developing a new site that has a navigation that is built dynamically via wordpress&#8217; <a href="http://codex.wordpress.org/Function_Reference/wp_list_pages">wp_list_pages</a> function</p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>  wp_list_pages<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sort_column=menu_order&amp;title_li=&amp;exclude=6'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>




<p>Navigation items that have submenus (css dropdowns) are still clickable, but for this website they have no content. I wanted a way to make those items unclickable. I was unable to find any way with wordpress&#8217; api, but I did find a way with jquery.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#navigation &gt; li &gt; a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">filter</span><span style="color: #009900;">&#40;</span>
    <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">siblings</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">1</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'href'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'#'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'onClick'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'return false'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>




<p>This sets the href to &#8216;#&#8217;, and onClick to &#8216;return false&#8217; of all navigation links that have submenus. Make sure to change the navigation id to reflect your site.</p>]]></content:encoded>
			<wfw:commentRss>http://www.galengrover.com/web-development/making-wordpress-parent-navigation-items-unclickable-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP template engines and why they&#8217;re useless</title>
		<link>http://www.galengrover.com/web-development/php-template-engines-and-why-theyre-useless/</link>
		<comments>http://www.galengrover.com/web-development/php-template-engines-and-why-theyre-useless/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 02:46:08 +0000</pubDate>
		<dc:creator>Galen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.galengrover.com/?p=369</guid>
		<description><![CDATA[For the past 4 or 5 years I&#8217;ve been a proponent of using PHP as it&#8217;s own template engine. I cringe every time I see someone using smarty, btemplate, fastTemplate, etc&#8230; because I&#8217;ve never had to do anything in my html that couldn&#8217;t be done with PHP. Never! Why add a layer to your app [...]]]></description>
			<content:encoded><![CDATA[<p>For the past 4 or 5 years I&#8217;ve been a proponent of using PHP as it&#8217;s own template engine. I cringe every time I see someone using smarty, btemplate, fastTemplate, etc&#8230; because I&#8217;ve never had to do anything in my html that couldn&#8217;t be done with PHP. Never! Why add a layer to your app if you don&#8217;t have to?</p>

<p>To illustrate this I&#8217;m going to show you a template written in a template engine, and then in PHP.</p>

<p>Here&#8217;s an example from the <a href="http://www.smarty.net/sampleapp/sampleapp_p5.php">Smarty website</a></p>


<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;table border=&quot;0&quot; width=&quot;300&quot;&gt;
    &lt;tr&gt;
        &lt;th colspan=&quot;2&quot; bgcolor=&quot;#d1d1d1&quot;&gt;Guestbook Entries (&lt;a href=&quot;{$SCRIPT_NAME}?action=add&quot;&gt;add&lt;/a&gt;)&lt;/th&gt;
    &lt;/tr&gt;
    {foreach from=$data item=&quot;entry&quot;}
        &lt;tr bgcolor=&quot;{cycle values=&quot;#dedede,#eeeeee&quot; advance=false}&quot;&gt;
            &lt;td&gt;{$entry.Name|escape}&lt;/td&gt;        
            &lt;td align=&quot;right&quot;&gt;{$entry.EntryDate|date_format:&quot;%e %b, %Y %H:%M:%S&quot;}&lt;/td&gt;        
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td colspan=&quot;2&quot; bgcolor=&quot;{cycle values=&quot;#dedede,#eeeeee&quot;}&quot;&gt;{$entry.Comment|escape}&lt;/td&gt;
        &lt;/tr&gt;
    {foreachelse}
        &lt;tr&gt;
            &lt;td colspan=&quot;2&quot;&gt;No records&lt;/td&gt;
        &lt;/tr&gt;
    {/foreach}
&lt;/table&gt;</pre></div></div>



<p>Here&#8217;s an example using PHP. <em>e() is an escaping function you write yourself.</em></p>


<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;table border=&quot;0&quot; width=&quot;300&quot;&gt;
    &lt;tr&gt;
        &lt;th colspan=&quot;2&quot; bgcolor=&quot;#d1d1d1&quot;&gt;Guestbook Entries (&lt;a href=&quot;{$SCRIPT_NAME}?action=add&quot;&gt;add&lt;/a&gt;)&lt;/th&gt;
    &lt;/tr&gt;
    &lt;?php if ( count( $data ) ): ?&gt;
    &lt;?php foreach( $data as $entry ): ?&gt;
        &lt;tr bgcolor=&quot;&lt;?php if( $i++ % 2 ): ?&gt;#dedede&lt;?php else: ?&gt;#eeeeee&lt;?php endif; ?&gt;&quot;&gt;
            &lt;td&gt;&lt;?= e( $entry['name'] ) ?&gt;&lt;/td&gt;        
            &lt;td align=&quot;right&quot;&gt;&lt;?= date( $entry['entryDate'], 'e b Y H M S' ) ?&gt;&lt;/td&gt;        
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td colspan=&quot;2&quot; bgcolor=&quot;&lt;?php if( $i % 2 ): ?&gt;#dedede&lt;?php else: ?&gt;#eeeeee&lt;?php endif; ?&gt;&quot;&gt;&lt;?= e( $entry['comment'] ) ?&gt;&lt;/td&gt;
        &lt;/tr&gt;
     &lt;?php endforeach; ?&gt;
     &lt;?php else: ?&gt;
        &lt;tr&gt;
            &lt;td colspan=&quot;2&quot;&gt;No records&lt;/td&gt;
        &lt;/tr&gt;
     &lt;?php endif; ?&gt;
&lt;/table&gt;</pre></div></div>




<p>As you can see there is only 2 added lines of code. I had to add an if statement to account for smarty&#8217;s foreachelse.  No added layer. No extra work for the interpreter. Just as readable (unless you&#8217;re a developer, then it will be more readable)</p>

<p>In closing read <a href="http://www.sitepoint.com/forums/showthread.php?p=498687">this post on the sitepoint forums</a>. Pay particular attention to Voostind&#8217;s replies (<a href="http://www.sitepoint.com/forums/showpost.php?p=498687&#038;postcount=15">#1</a>, <a href="http://www.sitepoint.com/forums/showpost.php?p=499483&#038;postcount=21">#2</a>, <a href="http://www.sitepoint.com/forums/showpost.php?p=500720&#038;postcount=29">#3</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.galengrover.com/web-development/php-template-engines-and-why-theyre-useless/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Merge query string</title>
		<link>http://www.galengrover.com/web-development/merge-query-string/</link>
		<comments>http://www.galengrover.com/web-development/merge-query-string/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 00:44:08 +0000</pubDate>
		<dc:creator>Galen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.galengrover.com/?p=184</guid>
		<description><![CDATA[Ever have a url like this&#8230;

index.php?dosearch=true&#038;page=1&#038;search[co_name]=best+buy&#038;search[address1]=12+main+st&#038;search[city]=bridgeport&#038;search[state_province_abbrev]=ct

&#8230;and wanted to create a link that would go to page=2, but keep the rest of the query string?

This recently happened to me so I created a function. Normally this would be a trivial call to array_merge, but I needed it to handle single-level arrays too.

I present to you mergeQuery


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function [...]]]></description>
			<content:encoded><![CDATA[<p>Ever have a url like this&#8230;</p>

<p>index.php?dosearch=true&#038;<strong>page=1</strong>&#038;search[co_name]=best+buy&#038;search[address1]=12+main+st&#038;search[city]=bridgeport&#038;search[state_province_abbrev]=ct</p>

<p>&#8230;and wanted to create a link that would go to <strong>page=2</strong>, but keep the rest of the query string?</p>

<p>This recently happened to me so I created a function. Normally this would be a trivial call to array_merge, but I needed it to handle single-level arrays too.</p>

<p>I present to you mergeQuery</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> mergeQuery<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$link</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #990000;">parse_str</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'QUERY_STRING'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$qry</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">parse_str</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$link</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lnk</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$mergedQry</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$qry</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lnk</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$mergedQry</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$var</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$val</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$val</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$val</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$val_k</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$val_v</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$finalQry</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'%s[%s]=%s&amp;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$var</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val_k</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val_v</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$finalQry</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'%s=%s&amp;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$var</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'?'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">rtrim</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$finalQry</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&amp;'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>




<p>Any variables you supply will overwrite variables from the current query string.</p>

<h3>Examples</h3>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?=</span> mergeQuery<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'page=1&amp;id=23'</span> <span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
&lt;a href=&quot;otherpage.php<span style="color: #000000; font-weight: bold;">&lt;?=</span> mergeQuery<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;page=5&amp;id=<span style="color: #006699; font-weight: bold;">{$data['id']}</span>&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;</pre></div></div>




<p>Maybe someone else will find some use for it</p>]]></content:encoded>
			<wfw:commentRss>http://www.galengrover.com/web-development/merge-query-string/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/web-development/returning-an-instance-of-a-class-from-pdo/</link>
		<comments>http://www.galengrover.com/web-development/returning-an-instance-of-a-class-from-pdo/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 04:54:55 +0000</pubDate>
		<dc:creator>Galen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[PDO]]></category>

		<guid isPermaLink="false">http://www.galengrover.com/?p=61</guid>
		<description><![CDATA[You can have PDO 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;;




But with fetch() you [...]]]></description>
			<content:encoded><![CDATA[<p>You can have PDO 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 fetchAll() 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 fetch() you must <em>initiate</em> it with setFetchMode</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/web-development/returning-an-instance-of-a-class-from-pdo/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>
