<?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>Learning Is Fun &#187; MySQL</title>
	<atom:link href="http://www.tanzilo.com/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tanzilo.com</link>
	<description>Talks on Web Technology and Better Product Development</description>
	<lastBuildDate>Sat, 17 Jan 2009 22:17:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>SQL Injection Prevention &amp; Protection in PHP &amp; MySQL with Example</title>
		<link>http://www.tanzilo.com/2008/11/14/sql-injection-prevention-protection-in-php-mysql-with-example/</link>
		<comments>http://www.tanzilo.com/2008/11/14/sql-injection-prevention-protection-in-php-mysql-with-example/#comments</comments>
		<pubDate>Fri, 14 Nov 2008 06:05:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[Prevention]]></category>
		<category><![CDATA[Protection]]></category>
		<category><![CDATA[Sanitize Input]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[SQL Injection]]></category>

		<guid isPermaLink="false">http://www.tanzilo.com/?p=7</guid>
		<description><![CDATA[What is SQL Injection? SQL Injection is the process when someone executes one or more SQL statements in your database without your knowledge to harm the data in your database. In this technique, someone exploits a security vulnerability in database application layer. This happens often when you ask for input(s) from the user and they [...]]]></description>
			<content:encoded><![CDATA[<p><span style="text-decoration: underline;"><strong>What is SQL Injection?</strong></span></p>
<p>SQL Injection is the process when someone executes one or more SQL statements in your database without your knowledge to harm the data in your database. In this technique, someone exploits a security vulnerability in database application layer. This happens often when you ask for input(s) from the user and they add extra SQL statement(s) with the valid input. We can illustrate this situation with two cases (1) user inputs are incorrectly filtered or (2) user submitted data is not strongly typed and one ore more of the inputs is executed unexpectedly.</p>
<p>This is possible because the user can submit any input and you may have no or weak user submitted input filtering coding, you can execute one more statements in your database at a time.<br />
For example,<br />
<span style="text-decoration: underline;"> Case 1</span>:</p>
<p>Executing one statement at a time is OK.</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
	$sqlStatement <span class="phpOperator">=</span> <span class="phpString">"SELECT * FROM customers where username=<span class="phpString">'james'</span><span class="phpText">;</span>"</span><span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p><span style="text-decoration: underline;">Case 2</span>:</p>
<p>Executing more than one statement at a time is also OK.</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
	$sqlStatement <span class="phpOperator">=</span> <span class="phpString">"DROP TABLE users<span class="phpText">;</span> UPDATE customers SET age=0; DELETE FROM customers where id<span class="phpOperator">&gt;</span>0;"</span><span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>Now an intruder can combine Case 1 and Case 2, run all the queries at a time and then delete the users table and harmfully update the customers table.</p>
<p>As a result, a successful SQL Injection occurs and harms the database information.</p>
<p><span style="text-decoration: underline;"><strong>Reasons behind SQL Injection</strong></span></p>
<p>In the following situations, SQL Injection happens.</p>
<ul>
<li>Incorrectly filtered escape characters</li>
<li>Incorrect type handling</li>
<li>Vulnerabilities inside the database server</li>
<li>Conditional Errors</li>
</ul>
<p><span style="text-decoration: underline;"><strong>Case 1: Incorrectly filtered escape characters</strong></span></p>
<p>You have a module that asks for user’s email address to send a temporary password to her email address when she forgets her password. So, the usual SQL query will be like this in this case:</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
	$sqlStatement <span class="phpOperator">=</span> <span class="phpString">"SELECT * FROM users WHERE username <span class="phpOperator">=</span> <span class="phpString">'" + $username + "'</span><span class="htmlText"> AND email </span><span class="phpOperator">=</span> <span class="phpString">'" + $email + "'</span> "</span><span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>But an intruder can extent this statement if they set the value for the $email variable in this way and delete the user table:</p>
<p>user@hostname.com&#8217;; DROP TABLE users; SELECT * FROM customers WHERE name LIKE &#8216;%</p>
<p>As a result, the final statement is something like:</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
	$sqlStatement <span class="phpOperator">=</span> <span class="phpString">"SELECT * FROM users WHERE username <span class="phpOperator">=</span> <span class="phpString">'james'</span><span class="htmlText"> AND email </span><span class="phpOperator">=</span> <span class="phpString">'user@hostname.com'</span><span class="phpText">;</span><span class="htmlText"> DROP TABLE users</span><span class="phpText">;</span> SELECT * FROM customers WHERE name LIKE <span class="phpString">'%'</span>"</span><span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>So, you see the intruder is deleting the users table easily. And as a result, you lose your users table and your system crashes since no user will be able to log in from now on. If you do not have a database backup, you loose everything.</p>
<p><span style="text-decoration: underline;"><strong>Case 2: Incorrect type handling</strong></span></p>
<p>Sometimes you definitely know the type of data. For example, the age of a customer is a numeric value, gender of a user as male or female, total amount of bill as double value.</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
	$sqlStatement <span class="phpOperator">=</span> <span class="phpString">"SELECT * FROM customers WHERE age <span class="phpOperator">=</span> "</span> <span class="phpOperator">+</span> $ageValue <span class="phpOperator">+</span> <span class="phpString">"<span class="phpText">;</span>"</span><span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>Now what if someone submits the <strong>$ageValue</strong> value as <span style="text-decoration: underline;">20; DROP TABLE users</span></p>
<p>The resulting sql is:</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
	$sqlStatement <span class="phpOperator">=</span> <span class="phpString">"SELECT * FROM customers WHERE age <span class="phpOperator">=</span> 20; DROP TABLE users<span class="phpText">;</span>"</span><span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>You know for sure that the value of will be always an integer. If you do not check if the value is really an integer, the intruder can add one or more statements with the value of $ageValue variable and harm your database.</p>
<p><span style="text-decoration: underline;"><strong>Case 3: Vulnerabilities inside the database server</strong></span></p>
<p>Although some people think that they can avoid SQL Injection just by using mysql_real_escape_string() function in PHP, they are wrong unfortunately. Built-in functions supplied with language package to work on database is sometimes vulnerable to database and thus are not successful to avoid the attack all the times.</p>
<p><span style="text-decoration: underline;"><strong>Case 4: Conditional Responses</strong></span></p>
<p>Using SQL Injection the user can easily bypass the sign in or log in process in your system. Let us give an example:</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
	$sqlStatement <span class="phpOperator">=</span> <span class="phpString">"SELECT * FROM users WHERE username <span class="phpOperator">=</span> <span class="phpString">'james'</span><span class="htmlText"> AND password </span><span class="phpOperator">=</span> <span class="phpString">'secret'</span><span class="htmlText"> OR </span><span class="phpNumber">1</span><span class="phpOperator">=</span><span class="phpNumber">1</span><span class="phpText">;</span>"</span><span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>Now you know satisfying only one condition is enough to enter the system. The condition OR 1=1 is always true. Thus, the intruder can fool the log in system.</p>
<p><span style="text-decoration: underline;"><strong>SQL Injection Prevention and/or Protection</strong></span></p>
<p>These are the techniques for preventing SQL Injection:</p>
<ol>
<li>Use Parameterized Query</li>
<li>Use Stored Procedure whenever applicable</li>
<li>Apply Regular expression to discard invalid inputs</li>
<li>Write and use Quote blocking  function</li>
<li>Hide detailed error messages from the user</li>
<li>Create a database user with less privileged role</li>
<li>Set the limitation for maximum value in your HTML form</li>
</ol>
<p><span style="text-decoration: underline;"><strong>1. Use Parameterized Query</strong></span></p>
<p>Rather than directly supplying the values in the SQL statement, let us supply the values in parameterized way as follows:</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
	$db_connection <span class="phpOperator">=</span><span class="phpKeyword"> new </span><span class="phpFunction">mysqli</span><span class="phpOperator">(</span><span class="phpString">"localhost"</span>, <span class="phpString">"user"</span>, <span class="phpString">"pass"</span>, <span class="phpString">"db"</span><span class="phpOperator">)</span><span class="phpText">;</span>
	$statement <span class="phpOperator">=</span> $db_connection<span class="phpOperator">-<span class="phpOperator">&gt;</span></span><span class="htmlText">prepare</span><span class="phpOperator">(</span><span class="phpString">"SELECT * FROM customers WHERE id <span class="phpOperator">=</span> <span class="phpOperator">?</span>"</span><span class="phpOperator">)</span><span class="phpText">;</span>
	$statement<span class="phpOperator">-<span class="phpOperator">&gt;</span></span><span class="htmlText">bind_param</span><span class="phpOperator">(</span><span class="phpString">"i"</span>, $id<span class="phpOperator">)</span><span class="phpText">;</span>
	$statement<span class="phpOperator">-<span class="phpOperator">&gt;</span></span><span class="htmlText">execute</span><span class="phpOperator">(</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>“i” stands for integer type<br />
“d” stands for double type<br />
“s” stands for string type<br />
“b” stands for a blob and will be send in packets</p>
<p><span style="text-decoration: underline;"><strong>2. Use Stored Procedure whenever applicable</strong></span></p>
<p>Using stored procedures can help your to reduce the attack risk too.</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
	$sqlStatement <span class="phpOperator">=</span> <span class="phpString">"
		CREATE PROCEDURE HUGEORDER
		<span class="phpOperator">(</span>
			id INT ,
			quantity INT,
			price DECIMAL<span class="phpOperator">(</span><span class="phpNumber">6</span>,2<span class="phpOperator">)</span>
		<span class="phpOperator">)</span>
		BEGIN
			DECLARE discount_percent DECIMAL<span class="phpOperator">(</span><span class="phpNumber">6</span>,2<span class="phpOperator">)</span><span class="phpText">;</span>
			<span class="htmlText">DECLARE discounted_price DECIMAL</span><span class="phpOperator">(</span><span class="phpNumber">6</span>,2<span class="phpOperator">)</span><span class="phpText">;</span>
			<span class="htmlText">SET discount_percent  </span><span class="phpOperator">=</span>  10;
			SET discounted_price <span class="phpOperator">=</span> price – discount_percent/100*price<span class="phpText">;</span>
			<span class="htmlText">IF quantity </span><span class="phpOperator">&gt;</span> 500 THEN
				SET discounted_price <span class="phpOperator">=</span> discounted_price - <span class="phpNumber">0</span><span class="phpOperator">.</span>25 * quantity<span class="phpText">;</span>
			<span class="htmlText">END  IF</span><span class="phpText">;</span>
			UPDATE fashion_products
			SET product_price <span class="phpOperator">=</span><span class="htmlText"> discounted_price WHERE product_id </span><span class="phpOperator">=</span><span class="htmlText"> id</span><span class="phpText">;</span>
			Select * from fashion_products<span class="phpText">;</span>
		<span class="htmlText">END</span><span class="phpText">;</span>
	"</span><span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p><span style="text-decoration: underline;"><strong>3. Apply Regular expression to discard invalid inputs</strong></span></p>
<p>Regular expression is very powerful process to find out the validity of the inputs. We can check whether the input is given in proper format. For example, here we validate data for a valid numeric value for customer age and reject any chance for SQL Injection.</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
<span class="phpKeyword">	if<span class="phpOperator">(</span></span><span class="phpOperator">!</span><span class="phpFunction">eregi</span><span class="phpOperator">(</span><span class="phpString">"^<span class="phpOperator">[</span>_a-z0-<span class="phpNumber">9</span>-<span class="phpOperator">]</span><span class="phpOperator">+</span><span class="phpOperator">(</span>\<span class="phpOperator">.</span><span class="phpOperator">[</span>_a-z0-<span class="phpNumber">9</span>-<span class="phpOperator">]</span><span class="phpOperator">+</span><span class="phpOperator">)</span>*@<span class="phpOperator">[</span>a-z0-<span class="phpNumber">9</span>-<span class="phpOperator">]</span><span class="phpOperator">+</span><span class="phpOperator">(</span>\<span class="phpOperator">.</span><span class="phpOperator">[</span>a-z0-<span class="phpNumber">9</span>-<span class="phpOperator">]</span><span class="phpOperator">+</span><span class="phpOperator">)</span>*<span class="phpOperator">(</span>\<span class="phpOperator">.</span><span class="phpOperator">[</span>a-z<span class="phpOperator">]</span><span class="phpOperator">{</span><span class="phpNumber">2</span>,3<span class="phpOperator">}</span><span class="phpOperator">)</span>$"</span>, $email<span class="phpOperator">)</span><span class="phpOperator">)</span>
	<span class="phpOperator">{</span>
		<span class="phpFunction">echo</span> <span class="phpString">'INVALID Email Address<span class="phpOperator">!</span>'</span><span class="phpText">;</span>
	<span class="phpKeyword">	return<span class="phpText">;</span></span>
	<span class="phpOperator">}</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>You can also user the built in PHP is_array(), is_bool(), is_double(), is_float(), is_int(), is_integer(), is_integer() etc functions to check if the user provided information is in proper format.</p>
<p><span style="text-decoration: underline;"><strong>4. Write and use Quote blocking  function</strong></span></p>
<p>If you are using PHP, mysql_real_escape_string function for each and every user given inputs. For example,</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
	$username <span class="phpOperator">=</span><span class="htmlText"> mysql_real_escape_string</span><span class="phpOperator">(</span>$username, $ dbLink<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>This is a very powerful built-in PHP function and will stop SQL Injection in most of the cases. I have used it for long time and found it performing great. You can try to inject SQL after you use the mysql_real_escape_string function and test if you can succeed any way. This powerful function rejects the possibility of many clever techniques used by the intruders.</p>
<p><span style="text-decoration: underline;"><strong>5. Hide detailed error messages from the user</strong></span></p>
<p>First of all avoid using the built-in MySQL mysql_error() function. The clever intruder can guess many things from the error message and sometimes the error message may show the connection parameters. Using mysql_error() function is good at development stage. But avoid or clean it when you run it in the real server for users or visitors.</p>
<p>The second thing is stop error reporting in PHP. This is simple and one line code.</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
	<span class="phpComment">// Turn off all error reporting
</span>	<span class="phpFunction">error_reporting</span><span class="phpOperator">(</span><span class="phpNumber">0</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>The third thing is better you give a customized error message. See an example:</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
<span class="phpKeyword">	if<span class="phpOperator">(</span></span><span class="phpOperator">!</span><span class="phpFunction">mysql_query</span><span class="phpOperator">(</span>$statement<span class="phpOperator">)</span><span class="phpOperator">)</span>
	<span class="phpOperator">{</span>
		<span class="phpFunction">echo</span> <span class="phpString">'We are sorry BUT The server is not responding<span class="phpOperator">.</span><span class="htmlText"> Please</span><span class="phpKeyword"> try </span>again later.'</span><span class="phpText">;</span>
	<span class="phpOperator">}</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>As a result, the user will not know what the error is and how it is. He will also not get any accidentally disclosed crucial information such as database name, table name, username etc.</p>
<p><span style="text-decoration: underline;"><strong>6. Create a database user with less privileged role</strong></span></p>
<p>In most cases, you will notice that the visitors do not need to delete or update any information. Think of a music selling site. The user can request for data (which is SELECT query) and make their orders (which are INSERT query). Even sometimes the SELECT operation is just OK in many sites.</p>
<p>So, create different users with different privileges. For admin grant all the permissions. But for a general visitor, grant only limited permission. For example, I am creating, two connection string for two users.</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
	$visitorDbLink <span class="phpOperator">=</span> <span class="phpFunction">mysql_connect</span><span class="phpOperator">(</span><span class="phpString">'host'</span>, <span class="phpString">'general_user'</span>, <span class="phpString">'general_user_pass'</span><span class="phpOperator">)</span><span class="phpText">;</span>
	$visitorDbLink <span class="phpOperator">=</span> <span class="phpFunction">mysql_connect</span><span class="phpOperator">(</span><span class="phpString">'host'</span>, <span class="phpString">'admin_user'</span>, <span class="phpString">'admin_pass'</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>And now we can user the $visitorDbLink link for regulating information connecting with the visitors and will use the $visitorDbLink link only for the administrator.</p>
<p><span style="text-decoration: underline;"><strong>7. Set the limitation for maximum value in your HTML form</strong></span></p>
<p>We can set that the username cannot be more than 10 chars. Try to use the “maxlength” property for HTML form. An example is like this:</p>
<pre class="html">
	<span class="htmlFormTag">&lt;input name=<span class="htmlAttributeValue">&quot;username&quot;</span> type=<span class="htmlAttributeValue">&quot;text&quot;</span> id=<span class="htmlAttributeValue">&quot;username&quot;</span> maxlength=<span class="htmlAttributeValue">&quot;10&quot;</span> /&gt;</span>
</pre>
<p>As a result, possibility of attack by intruder somewhat goes down since he cannot input enough harmful SQL statement input. But it is not very helpful though and the user can bypass this creating his own form and submitting the custom form.</p>
<p><span style="text-decoration: underline;"><strong>8. Using a PHP Framework</strong></span></p>
<p>If you are using a PHP framework like <a title="CodeIgniter" href="http://codeigniter.com/" target="_blank">CodeIgniter</a> or <a title="CakePHP" href="http://cakephp.org/" target="_blank">CakePHP</a>, the framework will protect you at maximum level from SQL injection. Complain of SQL Injection after using a PHP framework is rare and I did not hear even one still now.</p>
<p>This kind of frameworks are Open Source, very matured and strong nowadays and they do each and everything possible such as from input filtering to URL rewrite. Learning them is easy although it take some time. You can get books and other online materials about this PHP frameworks.</p>
<p>If you are an advanced PHP coder or developer, my suggestion is you better code using a PHP framework you like.</p>
<p>These are all I know about SQL Injection.</p>
<p>Thanks for reading.</p>
<p><map name='google_ad_map_7_eaab367e2f0158c1'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/7?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_7_eaab367e2f0158c1' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=7&amp;url= http%3A%2F%2Fwww.tanzilo.com%2F2008%2F11%2F14%2Fsql-injection-prevention-protection-in-php-mysql-with-example%2F' /></p>]]></content:encoded>
			<wfw:commentRss>http://www.tanzilo.com/2008/11/14/sql-injection-prevention-protection-in-php-mysql-with-example/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; MySQL: Unicode solution to Chinese, Russian or any language</title>
		<link>http://www.tanzilo.com/2008/10/13/php-mysql-unicode-solution-to-chinese-russian-or-any-language/</link>
		<comments>http://www.tanzilo.com/2008/10/13/php-mysql-unicode-solution-to-chinese-russian-or-any-language/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 12:07:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Bangla]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Store]]></category>
		<category><![CDATA[Text]]></category>
		<category><![CDATA[Unicode]]></category>

		<guid isPermaLink="false">http://www.tanzilo.com/?p=12</guid>
		<description><![CDATA[Hey Guys, I am a Freelance Web Developer and my main tools are PHP &#38; MySQL. Few days ago, I got a Chinese project where I had to develop a Real Estate site in Chinese language. You know we often build websites in English and Databases are in English too. So, the default configuration in [...]]]></description>
			<content:encoded><![CDATA[<p>Hey Guys,</p>
<p>I am a Freelance Web Developer and my main tools are PHP &amp; MySQL. Few days ago, I got a Chinese project where I had to develop a Real Estate site in Chinese language. You know we often build websites in English and Databases are in English too. So, the default configuration in MySQL works fine everytime.</p>
<p>But when it comes a language other than English, many people do not know what to do. Well. When I started the project, I did not even know that the default MySQL settings will not work for the Chinese language. So, I started searching for a stable solution where my program will support any language for adding, updating and searching data from the MySQL database.</p>
<p>And Yeah.<br />
I found it!</p>
<p>OK.</p>
<p>Let us see the solution now.<br />
It is very very simple.</p>
<p><strong>Step One: SET THE CHARSET TO UTF-8 IN THE HEAD SECTION </strong></p>
<p>First of all, the browser needs to know that you are going to display or use Unicode in this page. So, go to your &lt;HEAD&gt;&lt;/HEAD&gt; section and set the charset to utf-8. So, the browser will be able to show the Unicode text without any error and smoothly. You can also copy and paste the line below:</p>
<pre class="html">
<span class="htmlOtherTag">&lt;meta http-equiv=<span class="htmlAttributeValue">&quot;Content-Type&quot;</span> content=<span class="htmlAttributeValue">&quot;text/html; charset=utf-8&quot;</span> /&gt;</span>
</pre>
<p><strong>Step Two: CREATING THE DATABASE</strong></p>
<p>When you create your (a) Database and (b) any Table in the database, set the <span style="text-decoration: underline;">Collation</span> of both of them to <span style="text-decoration: underline;">utf8_unicode_ci</span> and you know it is very easy if you are using phpMyAdmin.</p>
<p><strong>Step Three: DATABASE INITIALIZATION</strong></p>
<p>When you initialize the database connection, please add the &#8220;extra lines&#8221;</p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
	<span class="phpFunction">define</span><span class="phpOperator">(</span><span class="phpString">'HOSTNAME'</span>, <span class="phpString">'localhost'</span><span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpFunction">define</span><span class="phpOperator">(</span><span class="phpString">'USERNAME'</span>, <span class="phpString">'database_user_name'</span><span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpFunction">define</span><span class="phpOperator">(</span><span class="phpString">'PASSWORD'</span>, <span class="phpString">'database_password'</span><span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpFunction">define</span><span class="phpOperator">(</span><span class="phpString">'DATABASE'</span>, <span class="phpString">'database_name'</span><span class="phpOperator">)</span><span class="phpText">;</span>
	$dbLink <span class="phpOperator">=</span> <span class="phpFunction">mysql_connect</span><span class="phpOperator">(</span>HOSTNAME, USERNAME, PASSWORD<span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpFunction">mysql_query</span><span class="phpOperator">(</span><span class="phpString">"SET character_set_results=utf8"</span>, $dbLink<span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpFunction">mb_language</span><span class="phpOperator">(</span><span class="phpString">'uni'</span><span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpFunction">mb_internal_encoding</span><span class="phpOperator">(</span><span class="phpString">'UTF-<span class="phpNumber">8</span>'</span><span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpFunction">mysql_select_db</span><span class="phpOperator">(</span>DATABASE, $dbLink<span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpFunction">mysql_query</span><span class="phpOperator">(</span><span class="phpString">"set names <span class="phpString">'utf8'</span>"</span>,$dbLink<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>But why are you adding the extra lines? Because you are letting the database know what kind of input you are going to work with soon.</p>
<p><strong>Step Four: INSERTING INPUTS/DATA IN THE DATABASE</strong></p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
	<span class="phpFunction">mysql_query</span><span class="phpOperator">(</span><span class="phpString">"SET character_set_client<span class="phpOperator">=</span>utf8"</span>, $dbLink<span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpFunction">mysql_query</span><span class="phpOperator">(</span><span class="phpString">"SET character_set_connection<span class="phpOperator">=</span>utf8"</span>, $dbLink<span class="phpOperator">)</span><span class="phpText">;</span>
	$sql_query <span class="phpOperator">=</span> <span class="phpString">"INSERT INTO
	TABLE_NAME<span class="phpOperator">(</span>field_name_one, field_name_two<span class="phpOperator">)</span>
	<span class="htmlText">VALUES</span><span class="phpOperator">(</span><span class="phpString">'field_value_one'</span>, <span class="phpString">'field_value_two'</span><span class="phpOperator">)</span>"</span><span class="phpText">;</span>
	<span class="phpFunction">mysql_query</span><span class="phpOperator">(</span>$sql_query, $dbLink<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>Why are you adding the first two lines for? Because the database should know what kind of data is going to be stored.</p>
<p><strong>Step Five: UPDAT</strong><strong>ING INPUTS/DATA IN THE DATABASE</strong></p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
	<span class="phpFunction">mysql_query</span><span class="phpOperator">(</span><span class="phpString">"SET character_set_client<span class="phpOperator">=</span>utf8"</span>, $dbLink<span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpFunction">mysql_query</span><span class="phpOperator">(</span><span class="phpString">"SET character_set_connection<span class="phpOperator">=</span>utf8"</span>, $dbLink<span class="phpOperator">)</span><span class="phpText">;</span>
	$sql_query <span class="phpOperator">=</span> <span class="phpString">"UPDATE TABLE_NAME
	SET field_name_one=<span class="phpString">'field_value_one'</span>, field_name_two<span class="phpOperator">=</span><span class="phpString">'field_value_two'</span>
	<span class="htmlText">WHERE id</span><span class="phpOperator">=</span><span class="phpString">'$id'</span><span class="phpText">;</span> "</span><span class="phpText">;</span>
	<span class="phpFunction">mysql_query</span><span class="phpOperator">(</span>$sql_query, $dbLink<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>So, you are adding the extra two lines before you run your query string as you are playing with Unicode.</p>
<p><strong>Step Six: SEARCHING DATA FROM THE DATABASE</strong></p>
<pre class="php">
<span class="phpScriptTag"><span class="phpOperator">&lt;</span><span class="phpOperator">?</span>php</span>
	<span class="phpFunction">mysql_query</span><span class="phpOperator">(</span><span class="phpString">"SET character_set_results=utf8"</span>, $dbLink<span class="phpOperator">)</span><span class="phpText">;</span>
	$sql_query <span class="phpOperator">=</span> <span class="phpString">"SELECT * FROM TABLE_NAME WHERE id<span class="phpOperator">=</span><span class="phpString">'$id'</span><span class="phpText">;</span> "</span><span class="phpText">;</span>
	$dbResult <span class="phpOperator">=</span> <span class="phpFunction">mysql_query</span><span class="phpOperator">(</span> $sql_query, $dbLink<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpScriptTag"><span class="phpOperator">?</span><span class="phpOperator">&gt;</span></span>
</pre>
<p>Adding the one extra line every time you search your Unicode data is enough.</p>
<p>OKKK.<br />
You are done. This should work smoothly for handling your data in any language does not matter it is Bangla (my mother tongue), Hindi, Chinese, French, German, Spanish, Russian, Arabian (Arabic), Urdu,  or any other language.</p>
<p>And do not forget to leave a comment if you have any. Because I need to update the post in case required.</p>
<p>Thanks for reading and please check if it works for you.</p>
<p><map name='google_ad_map_12_eaab367e2f0158c1'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/12?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_12_eaab367e2f0158c1' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=12&amp;url= http%3A%2F%2Fwww.tanzilo.com%2F2008%2F10%2F13%2Fphp-mysql-unicode-solution-to-chinese-russian-or-any-language%2F' /></p>]]></content:encoded>
			<wfw:commentRss>http://www.tanzilo.com/2008/10/13/php-mysql-unicode-solution-to-chinese-russian-or-any-language/feed/</wfw:commentRss>
		<slash:comments>88</slash:comments>
		</item>
	</channel>
</rss>
