<?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>Web Log of Nazly &#187; MySQL</title>
	<atom:link href="http://www.nazly.net/category/technology/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nazly.net</link>
	<description>Personal Weblog of Nazly Ahmed : Web Developer. PHP Addict. Wordpress Hacker. FOSS Enthusiast. (Micro)Blogger. Cricket Fanatic. Husband. Dad.</description>
	<lastBuildDate>Tue, 24 Aug 2010 16:22:10 +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>Protect your website against SQL injection</title>
		<link>http://www.nazly.net/protect-your-website-against-sql-injection/</link>
		<comments>http://www.nazly.net/protect-your-website-against-sql-injection/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 12:07:35 +0000</pubDate>
		<dc:creator>Nazly</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[injection]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[website]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wp]]></category>

		<guid isPermaLink="false">http://www.nazly.net/?p=752</guid>
		<description><![CDATA[SQL injection is one of the deadliest techniques attackers use to exploit the weakness in your database code of your website. Regardless of the technology/scripting language you must make sure your code is 100% perfect against SQL injection. Here I (&#8230;)<p><a href="http://www.nazly.net/protect-your-website-against-sql-injection/">Read the rest of this entry &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/SQL_injection" target="_blank"><strong>SQL injection</strong></a> is one of the deadliest techniques attackers use to exploit the weakness in your database code of your website. Regardless of the technology/scripting language you must make sure your code is 100% perfect against SQL injection.</p>
<p>Here I will use <a href="http://www.php.net" target="_blank"><strong>PHP</strong></a> and <a href="http://www.mysql.com" target="_blank"><strong>MySQL</strong></a> examples for its wide usage and also I&#8217;m much more comfortable with it. </p>
<p>Here is a basic PHP code that most developers will come up with to access the MySQL DB and get the record of a particular username submitted from a form in our website.</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>
<span style="color: #666666; font-style: italic;"># Get posted username value
</span><span style="color: #000088;">$userName</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;usname&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;"># MySQL query string to get the record of the user
</span><span style="color: #000088;">$queryStr</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM users WHERE usname = '<span style="color: #006699; font-weight: bold;">$userName</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;"># Output the string for debugging
</span><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$queryStr</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;"># Execute the MySQL query
</span><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$queryStr</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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>For example if the username that was submitted is <b>nazly</b> the code will output the following query and execute it.</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> users <span style="color: #990099; font-weight: bold;">WHERE</span> usname <span style="color: #CC0099;">=</span> <span style="color: #008000;">'nazly'</span></pre></div></div>

<p>While the query works perfectly and returns the record of that particular user, a attacker can exploit this code by injecting SQL using the submission form. </p>
<p>For example if the attacker submits <b>&#8216; OR &#8216;t&#8217;='t</b> instead of the username the query will be formed like this.</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> users <span style="color: #990099; font-weight: bold;">WHERE</span> usname <span style="color: #CC0099;">=</span> <span style="color: #008000;">''</span> <span style="color: #CC0099; font-weight: bold;">OR</span> <span style="color: #008000;">'t'</span><span style="color: #CC0099;">=</span><span style="color: #008000;">'t'</span></pre></div></div>

<p>When this query is executed, it will return all the records in the database since t=t will be TRUE always. The impact it will have on the website will be depend on the code after executing the query. <strong>But the important thing is someone can make the query behave differently than what we actually expected from it</strong>.</p>
<p>It can become deadlier than that if someone submits the following instead of the username<br />
<b>a&#8217;;DROP TABLE users; SELECT * FROM userinfo WHERE &#8216;t&#8217; = &#8216;t</b><br />
The query for the above value will look like this</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> users <span style="color: #990099; font-weight: bold;">WHERE</span> usname <span style="color: #CC0099;">=</span> <span style="color: #008000;">'a'</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">TABLE</span> users<span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> userinfo <span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #008000;">'t'</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">'t'</span></pre></div></div>

<p>If the above query is executed, it will delete the whole users table. Similarly an attacker can inject any type of SQL code to modify/delete your tables in the database.</p>
<p>It is a huge security flaw in your code but newbies and even some experienced developers don&#8217;t understand the depth of problem. So developers should make sure to take precautionary measures against it.</p>
<p>In PHP you can use the <a href="http://www.php.net/mysql_real_escape_string" target="_blank"><strong>mysql_real_escape_string()</strong></a> function for this task. This function will escape any special characters in the string to be used in a SQL statement.</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>
<span style="color: #666666; font-style: italic;"># Get posted username value by escaping special characters
</span><span style="color: #000088;">$userName</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;usname&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;"># MySQL query string to get the record of the user
</span><span style="color: #000088;">$queryStr</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM users WHERE usname = '<span style="color: #006699; font-weight: bold;">$userName</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;"># Output the string for debugging
</span><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$queryStr</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;"># Execute the MySQL query
</span><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$queryStr</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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>If you try to inject SQL to this example, it will have no affect to the Database since the use of this function</p>
<p>If you are a developing a <a href="http://www.wordpress.org"><strong>WordPress</strong></a> plugin for your website, you must make sure to protect the site against SQL injection as well. Since WordPress has its own class for database manipulation you should use the methods available in WordPress.</p>
<p>The escape() function in the <a href="http://codex.wordpress.org/Function_Reference/wpdb_Class" target="_blank"><strong>WPDB</strong></a> class is much similar to using the standard mysql_real_escape_string() 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>
<span style="color: #000000; font-weight: bold;">function</span> myWpPluginFunc<span style="color: #009900;">&#40;</span><span style="color: #000088;">$usName</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$u</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">escape</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$usName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM users WHERE usname = '<span style="color: #006699; font-weight: bold;">$u</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>But there is a better option available in WordPress. Rather than escaping individual values you can format the SQL statement and then use the prepare() function in the WPDB class to escape the special characters. The syntax is similar to using sprintf(). Using the prepare() function, the developer is sure that all values are escaped. So less chance for errors.</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>
<span style="color: #000000; font-weight: bold;">function</span> myWpPluginFunc<span style="color: #009900;">&#40;</span><span style="color: #000088;">$usName</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$qstr</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM users WHERE usname = <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$usName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$qstr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>For more check out <a href="http://codex.wordpress.org/Data_Validation#Database" target="_blank"><strong>Data Validation in WordPress</strong></a>.</p>
<p>I wish MySQL functions in PHP had a similar function like WordPress&#8217;s prepare()</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nazly.net/protect-your-website-against-sql-injection/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Restoring Large MySQL dumps</title>
		<link>http://www.nazly.net/restoring-large-mysql-dumps/</link>
		<comments>http://www.nazly.net/restoring-large-mysql-dumps/#comments</comments>
		<pubDate>Mon, 26 Nov 2007 06:37:52 +0000</pubDate>
		<dc:creator>Nazly</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[restore]]></category>

		<guid isPermaLink="false">http://blog.nazly.net/restoring-large-mysql-dumps/</guid>
		<description><![CDATA[A lot of things influenced me to write this post. In fact I wanted to write on this sometime back but finally I was able to squeeze some time. Lately I have been moving data between servers and the main (&#8230;)<p><a href="http://www.nazly.net/restoring-large-mysql-dumps/">Read the rest of this entry &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>A lot of things influenced me to write this post. In fact I wanted to write on this sometime back but finally I was able to squeeze some time. Lately I have been moving data between servers and the main problem I had was with large MySQL dumps because I have been using <a href="http://www.phpmyadmin.net" target="_blank">PHPMyAdmin</a> for most of the MySQL operations and its one of the best tools available and most importantly its web-based. Even on my local development environment I&#039;m comfortable with using PHPMyAdmin and on the web servers it can be very handy if the server is a shared hosting server. I would rather recommend using the command line client utilities that MySQL offers for import/export operations because its the safest. But you will need SSH access to your server. If you do have SSH access don&#039;t hesitate to choose this method above the others.</p>
<p><b>Export/Backup</b><br />
<br />Using the mysqldump client it is possible to backup a database into a SQL file which will contain SQL statements that can recreate the database tables when restored.</p>
<p>The following command from shell can be executed to backup a specific database<br />
</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysqldump <span style="color: #660033;">-u</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>username<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #660033;">-p</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>password<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>databasename<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>backupfile.sql<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p><a href="http://dev.mySQL.com/doc/refman/5.0/en/mysqldump.html" target="_blank">Click Here</a> for more options on using mysqldump</p>
<p><b>Import/Restore</b><br />
<br />You can use this command from shell to restore the database using the SQL dump file<br />
</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysql <span style="color: #660033;">-u</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>username<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #660033;">-p</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>password<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>databasename<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>backupfile.sql<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>So thats quite basic and safest ways to import/export. But then you might ask whats the options you have if you are on a shared server and do not have SSH access. Well then PHPMyAdmin is the only choice available coz its web-based. Most servers have PHPMyAdmin as an option in the Server&#039;s Control Panel. Worst case if you don&#039;t have it or cannot find it you simply can download the source from <a href="http://www.phpmyadmin.net" target="_blank">www.phpmyadmin.net</a> and upload the files via FTP and set it up. Installation is quite simple if you follow the Documentation.txt file. Once it is setup you can create databases etc. When you have selected a database from the left panel there will be tabs called SQL and Export. Using the SQL tab you can restore the database using your SQL dump file. Similarly you can use the Export option to export the data to a SQL file.</p>
<p>But then again you come across problems when you have a large MySQL dump. Exporting a large database won&#039;t be a problem but there are times that the SQL dump file tends to get corrupted for various reasons. Importing a large SQL dump file would create a problems coz with default installations there is a 2MB upload limit. This is not a PHPMyAdmin limit. This limit is set in the PHP configuration. To increase this upload limit you have to change the <i>post_max_size</i> and <i>upload_max_filesize</i> directives in the php.ini and then you can restore a large SQL dump. But if you are on a shared hosting server its highly unlikely that you can change the directives in the php.ini file. So thats where most people get stuck. When you are moving from your local machine to the server this is one problem you will face. Similar problem I faced when I had to move a large database from a server that I could SSH to a shared hosting server. I dumped the database to a SQL file using  mysqldump command line utility and then when I tried to restore using PHPMyAdmin there was this upload limit. Arghh.. At that time I simply split the file manually into smaller files which are less than 2MB and uploaded one by one. It came to about 7 files at that time so didn&#039;t really bother about splitting them manually. This is a dirty trick but still effective but I won&#039;t suggest you to use this method. At a later time when I came across a similar instance I planned to write a tiny PHP script that would do the job. But thankfully I got a new server that I could SSH into. </p>
<p>So things can get bit messy at these situations so gotta figure out ways to overcome those with the limited resources we have. Lately I found <a href="http://www.ozerov.de/bigdump.php" target="_blank">BigDump: Staggered MySQL Dump Importer</a> which seems to do the job on the web servers with hard runtime limit. So I guess I have more choices now. I haven&#039;t tried out this yet. Hopefully can play around with this next time when I have tight limits.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nazly.net/restoring-large-mysql-dumps/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Copying records in MySQL</title>
		<link>http://www.nazly.net/copying-records-in-mysql/</link>
		<comments>http://www.nazly.net/copying-records-in-mysql/#comments</comments>
		<pubDate>Thu, 17 May 2007 06:12:05 +0000</pubDate>
		<dc:creator>Nazly</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[copy]]></category>
		<category><![CDATA[record]]></category>

		<guid isPermaLink="false">http://blog.nazly.net/copying-records-in-mysql/</guid>
		<description><![CDATA[Copying records from one table to another can be a very basic requirement. But writing queries to perform this task can be bit of a work around. But there is a very simple query to get this done. Its by (&#8230;)<p><a href="http://www.nazly.net/copying-records-in-mysql/">Read the rest of this entry &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Copying records from one table to another can be a very basic requirement. But writing queries to perform this task can be bit of a work around. But there is a very simple query to get this done.<br />
<br />Its by using INSERT &#8230; SELECT<br />
<br /><a href="http://dev.mysql.com/doc/refman/5.0/en/insert-select.html" target="_blank">http://dev.mysql.com/doc/refman/5.0/en/insert-select.html</a></p>
<p>Copy one record from a table to another<br />
</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;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> <span style="color: #008000;">`dest<span style="color: #008080; font-weight: bold;">_</span>table`</span> <span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> source_table <span style="color: #990099; font-weight: bold;">WHERE</span> id <span style="color: #CC0099;">=</span> <span style="color: #008000;">'10'</span></pre></td></tr></table></div>

<p>It can be very simple as this. Even multiple records can be copied from a single table or several tables. If I&#039;m not mistaken INSERT &#8230; SELECT works on MySQL versions 4.1 and above.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nazly.net/copying-records-in-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
