<?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>Haiders WebSpace &#187; SQL</title>
	<atom:link href="http://blog.creativeitp.com/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.creativeitp.com</link>
	<description>Let&#039;s talk about IT</description>
	<lastBuildDate>Sat, 27 Feb 2010 13:15:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Retrieve data as UTF-8 from MySQL DB</title>
		<link>http://blog.creativeitp.com/posts-and-articles/phpbb/retrieve-data-as-utf-8-from-mysql-db/</link>
		<comments>http://blog.creativeitp.com/posts-and-articles/phpbb/retrieve-data-as-utf-8-from-mysql-db/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 15:43:39 +0000</pubDate>
		<dc:creator>Haider al-Khateeb</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[phpBB]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://blog.creativeitp.com/?p=100</guid>
		<description><![CDATA[The image above is showing part of a small hack I coded for a phpBB board to show topics from selected sections. Those question marks were suppose to be Arabic letters, but after a recent update to phpBB the hack failed to use the board config file to connect so I had to set up [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">
<p style="text-align: center;">
<div id="attachment_102" class="wp-caption aligncenter" style="width: 478px"><img src="http://blog.creativeitp.com/wp-content/uploads/2010/01/charset-problem02.jpg" alt="" title="Charset Problem" width="468" height="58" class="size-full wp-image-102" /><p class="wp-caption-text">Charset Problem</p></div>
</p>
<p style="text-align: center;">
<p style="text-align: justify;">The image above is showing part of a small hack I coded for a phpBB board to show topics from selected sections. Those question marks were suppose to be Arabic letters, but after a recent update to phpBB the hack failed to use the board config file to connect so I had to set up my own direct connection to the DB.</p>
</p>
<p style="text-align: justify;">The connection was fine, but I got &#8220;?????&#8221; instead of something I can read in Arabic. After some tests I figured out, though the MySQL DB is using UTF-8 as a character set, which means it can handle the language efficiently, I had to set the character set to UTF-8 via PHP for every query using the following code:</p>
<p style="text-align: justify;">
<p style="text-align: justify;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SET CHARACTER SET utf8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">
<p style="text-align: justify;">What is even better is setting the character set at once with a single line of code for the whole connection by using the following code:</p>
<p><span id="more-100"></span></p>
<p style="text-align: justify;">
<p style="text-align: justify;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SET CHARACTER SET 'utf8'&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">
<p style="text-align: justify;">This code should be used directly after the connection, as in the following example:</p>
<p style="text-align: justify;">
<p style="text-align: justify;">

<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;">$db</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'DB_USER'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'DB_PASSWORD'</span><span style="color: #009900;">&#41;</span> 
or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not connect&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_NAME'</span><span style="color: #339933;">,</span><span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not connect&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
  <span style="color: #666666; font-style: italic;">// Set character set to UTF-8</span>
  <span style="color: #666666; font-style: italic;">//</span>
  <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SET CHARACTER SET 'utf8'&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">
<p style="text-align: justify;">Now I got my topics in readable Arabic once again!</p>
<p style="text-align: justify;">
<p style="text-align: center;">
<div id="attachment_101" class="wp-caption aligncenter" style="width: 476px"><img src="http://blog.creativeitp.com/wp-content/uploads/2010/01/charset-problem01.jpg" alt="" title="Charset Problem Solved" width="466" height="57" class="size-full wp-image-101" /><p class="wp-caption-text">Charset Problem Solved</p></div>
<p style="text-align: justify;">
]]></content:encoded>
			<wfw:commentRss>http://blog.creativeitp.com/posts-and-articles/phpbb/retrieve-data-as-utf-8-from-mysql-db/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
