<?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>Nazly [Web Log] &#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>Mon, 24 Oct 2011 08:06:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Optimizing the MySQL tables of a WordPress Website</title>
		<link>http://www.nazly.net/optimizing-the-mysql-tables-of-a-wordpress-website/</link>
		<comments>http://www.nazly.net/optimizing-the-mysql-tables-of-a-wordpress-website/#comments</comments>
		<pubDate>Fri, 04 Feb 2011 09:14:31 +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[blog]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[execute]]></category>
		<category><![CDATA[optimize]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[table]]></category>
		<category><![CDATA[website]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.nazly.net/?p=1329</guid>
		<description><![CDATA[If you are running a WordPress Website or a Blog where the content is updated and/or deleted frequently, you will need to optimize the MySQL tables more often than not so that you don&#8217;t run into database issues and down (&#8230;)<p><a href="http://www.nazly.net/optimizing-the-mysql-tables-of-a-wordpress-website/">Read the rest of this entry &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.nazly.net/wp-content/uploads/2011/02/php-mysql-wordpress.jpg" alt="" title="php-mysql-wordpress" width="250" height="150" class="alignleft size-full wp-image-1336" /> If you are running a <a href="http://wordpress.org" target="_blank">WordPress</a> Website or a Blog where the content is updated and/or deleted frequently, you will need to optimize the <a href="http://mysql.com" target="_blank">MySQL</a> tables more often than not so that you don&#8217;t run into database issues and down times. This also helps to maintain the average response time of MySQL queries.</p>
<p><span id="more-1329"></span>When executed, the following <a href="http://php.net" target="_blank">PHP</a> script will optimize the tables which requires optimization in your WordPress database. Place this file in your WordPress&#8217;s root directory.</p>
<p>You can run this manually or the ideal scenario would be to setup a cron job to execute it in a given time interval based on your requirement..</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: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;wp-config.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span>DB_HOST<span style="color: #339933;">,</span> DB_USER<span style="color: #339933;">,</span> DB_PASSWORD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span>DB_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$selQuery</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SHOW TABLE STATUS FROM `&quot;</span><span style="color: #339933;">.</span>DB_NAME<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;`&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$resSel</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$selQuery</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: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resSel</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Data_free&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>    
        <span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y-m-d H:i:s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; - Optimized : &quot;</span>
                <span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Name&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; ( &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Data_free&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; )<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$optQuery</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;OPTIMIZE TABLE `&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Name&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;`&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$optQuery</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: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;---------------<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.nazly.net/optimizing-the-mysql-tables-of-a-wordpress-website/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Basics of maintaing a WordPress site</title>
		<link>http://www.nazly.net/basics-of-maintaing-a-wordpress-site/</link>
		<comments>http://www.nazly.net/basics-of-maintaing-a-wordpress-site/#comments</comments>
		<pubDate>Thu, 18 Nov 2010 17:36:45 +0000</pubDate>
		<dc:creator>Nazly</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[LAMP]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[alert]]></category>
		<category><![CDATA[cms]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[maintenance]]></category>
		<category><![CDATA[monitor]]></category>
		<category><![CDATA[notificaion]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[uptime]]></category>
		<category><![CDATA[website]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.nazly.net/?p=903</guid>
		<description><![CDATA[WordPress is simple, yet powerful. Those are the key ingredients why it is popular among the Bloggers and Web Developers alike. WordPress will always be the first choice as a blogging platform. In the recent times it has moved far (&#8230;)<p><a href="http://www.nazly.net/basics-of-maintaing-a-wordpress-site/">Read the rest of this entry &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-1072" title="wordpress-logo" src="http://www.nazly.net/wp-content/uploads/2011/06/wordpress-logo.png" alt="" width="250" height="155" /> <a href="http://wordpress.org">WordPress</a> is <strong>simple</strong>, yet <strong>powerful</strong>. Those are the key ingredients why it is popular among the Bloggers and Web Developers alike. WordPress will always be the first choice as a blogging platform. In the recent times it has moved far from being just a blogging engine. Most Web developers choose WordPress as their primary <a href="http://en.wikipedia.org/wiki/Content_management_system">CMS</a> of choice ahead of other popular Content Management Systems. Regardless of you being a Blogger or a Web Developer, the manner you maintain your WordPress files and database will play an important role in running a successful website.</p>
<p><span id="more-903"></span>Maintaining and monitoring your WordPress site requires performing certain tasks on a regular basis. The term regular will depend on how often your Website gets updated. Depending on the frequency you will need to plan this out. If you run a Website that gets updated daily (eg: news), then taking daily backups is a high priority. Performing these tasks manually can become a tedious process. There are plenty of tools and techniques available to automate them.</p>
<h3>Monitoring a Website</h3>
<p>You might want to use a <a href="http://www.google.lk/search?q=Website+Monitoring+Service">Website Monitoring Service</a> to check whether your website is up and running. There are plenty of services over the Internet if you Google for it. These services offer monitoring your Web Server (HTTP), FTP, Mail Server (POP &amp; SMTP), SSL, DNS and custom TCP ports on regular intervals. If any of these don&#8217;t respond, the service will alert you via Email or SMS. Most of them offer limited options for free while you need to pay based on the additional services you require. This type of monitoring is not WordPress specific and can apply to any Wesbite.</p>
<p>If you are a developer you can code your own monitoring system which will be cost efficient and can be expandable according to your monitoring requirements. Using <a href="http://php.net/manual/en/function.fsockopen.php">socket functions</a> and tools like <a href="http://www.php.net/manual/en/ref.curl.php">curl</a> can assist in doing that. </p>
<p>In any of the case above, you will need to specify the port of the service you wish to monitor. In certain cases, the port can occasionally differ from the default port that particular service would run depending on how it is configured on your Server. So you need to specify the correct one. </p>
<p>There is one instance that most of the Bloggers/Web Developers fail to address. While your Web Server/Apache might run smoothly, there is a possibility that your database might not respond. This can happen when your DB gets corrupted, privileges get messed up or your MySQL Server crashes for some reason. In this case WordPress will send you this ugly message on a plain white page. </p>
<p><img src="http://www.nazly.net/wp-content/uploads/2011/06/databaseerror.jpg" alt="" title="databaseerror" width="394" height="37" class="alignnone size-full wp-image-1097" /></p>
<p>It&#8217;s not suitable to show an error message of this nature to your visitors. Most importantly the HTTP status code for this response is 200, which means Search Engines will pick it up as your content for the site. If this message hangs on for long, you will get indexed on Google with this Error Message.</p>
<p>Here is how you can display a custom error message with your design template and get notified at the same time when your DB doesn&#8217;t respond.</p>
<p>Create a file called <strong>db-error.php</strong> and store that file under the wp-contents folder.<br />
The contents of the file should be..</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;HTTP/1.0 500 Internal Server Error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;webmaster@mydomain.com&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;DB fail&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Down at : &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y-m-d H:i:s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;html&gt;Your customer error message with yout HTML template&lt;/html&gt;&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It&#8217;s that simple..!</p>
<p>WordPress will execute the above script when the DB doesn&#8217;t respond. The first line will send the HTTP response code 500 (Internal Server Error) so that Search Engines won&#8217;t index it. The next line will send an email to you, notifying the time the DB went down. Third line will output the HTML of your template where you can include a custom error message. You can extend this piece of code to fit into your needs. Instead of Email, you could use SMS/Twitter to get instantly notified. </p>
<h3>Backup</h3>
<p>Taking regular backups of your WordPress installation and most importantly the database regularly will help to restore your Website within minutes if any data loss occurs. A hard disk crash, server failure or even a hacker wiping out all your data can be some of the reasons how a data loss could occur. While your hosting provider might do the backup for you, its always good to have your own. If you take regular backups, you are never a loser.</p>
<p>One of the important folders to backup is the wp-contents folder, but I would advise you to backup the whole WordPress installation directory. You will also need to backup the MySQL database. A simple MySQL dump would do. If you are the average blogger, then there are quite a lot of <a href="http://wordpress.org/extend/plugins/search.php?q=backup">WordPress plugins for backup</a> which would do the job for you. If you have shell access, I would advice you to write a shell script that would backup the files and database. Add a cron job to execute the script based on how frequent you wish to take backups. If you don&#8217;t have shell access, you can try using the options in your Web Hosting Control Panel. </p>
<h3>Security</h3>
<p>When it comes to security, applying the correct patches and upgrading to the newest version of the core and plugins of WordPress will prevent the hackers  from trying to exploit the vulnerabilities in your Website. While this can be automated, it is advisable you do this process manually to reduce complications that may arise after an upgrade. It will depend on the plugins you use and the purpose you use it for. Golden rule before upgrade is to backup your WordPress files and DB so that you can restore it if something goes wrong.</p>
<p>Before choosing the right plugin to use, make sure to check the compatibility of the plugin with the version of WordPress you are running. Also make sure to check the ratings of the plugin. All these information is available on the plugin&#8217;s page.</p>
<p>Good luck..!!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nazly.net/basics-of-maintaing-a-wordpress-site/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<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><span id="more-752"></span></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>
		<item>
		<title>MySQL GUI tools</title>
		<link>http://www.nazly.net/mysql-gui-tools/</link>
		<comments>http://www.nazly.net/mysql-gui-tools/#comments</comments>
		<pubDate>Tue, 20 Sep 2005 11:39:45 +0000</pubDate>
		<dc:creator>Nazly</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[gui]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://blog.nazly.net/mysql-gui-tools/</guid>
		<description><![CDATA[I have been playing around with the MySQL GUI tools lately specially with the MySQL Query Browser and its really easy to play around with SQL queries. I did use PHPMyAdmin effectively though I simply like the Query Browser. MySQL (&#8230;)<p><a href="http://www.nazly.net/mysql-gui-tools/">Read the rest of this entry &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>I have been playing around with the MySQL GUI tools lately specially with the MySQL Query Browser and its really easy to play around with SQL queries. I did use PHPMyAdmin effectively though I simply like the Query Browser. MySQL Migration Toolkit and MySQL Administrator are the other two GUI tools available. If you haven&#039;t started on it, go get &#039;em <img src="http://www.nazly.net//smiles/icon_wink.gif" alt="[Wink]" />. You can download them from <a href="http://dev.mysql.com/downloads/" target="_blank">http://dev.mysql.com/downloads/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.nazly.net/mysql-gui-tools/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Export MySQL database to an MS Excel format</title>
		<link>http://www.nazly.net/export-mysql-database-to-an-ms-excel-format/</link>
		<comments>http://www.nazly.net/export-mysql-database-to-an-ms-excel-format/#comments</comments>
		<pubDate>Tue, 05 Jul 2005 02:09:55 +0000</pubDate>
		<dc:creator>Nazly</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[export]]></category>
		<category><![CDATA[microsoft]]></category>

		<guid isPermaLink="false">http://blog.nazly.net/export-mysql-database-to-an-ms-excel-format/</guid>
		<description><![CDATA[Here is an updated version of the script I had at PHP-Help.net which exports a specified MySQL table. After couple of requests I got, I made some changes to the script to download all the tables of a specific database. (&#8230;)<p><a href="http://www.nazly.net/export-mysql-database-to-an-ms-excel-format/">Read the rest of this entry &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Here is an updated version of the script I had at <a href="http://www.php-help.net/sources-php/export.mysql.table.to.ms.excel.316.html" target="_blank">PHP-Help.net</a> which exports a specified MySQL table. After couple of requests I got, I made some changes to the script to  download all the tables of a specific database. Let me know if u catch any bugs.</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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
</pre></td><td 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;">#Name of the Database to export can be sent via GET variable called 'db'
</span><span style="color: #000088;">$dbToExport</span><span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;db&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>?<span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;db&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;******&quot;</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;Connection to MySQL failed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbToExport</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;Couldn't connect to DB&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Get all tables in the database
</span><span style="color: #000088;">$mTblQuery</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;show tables&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mTblResult</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mTblQuery</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$dataStr</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">#Loop through the table names
</span><span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tblRow</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mTblResult</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">#Store output of the table name
</span>	<span style="color: #000088;">$dataStr</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;Table : <span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$tblRow</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Tables_in_&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$dbToExport</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">#Select all records from the table
</span>	<span style="color: #000088;">$mQuery</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;select * from `&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$tblRow</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;Tables_in_&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$dbToExport</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;`&quot;</span><span style="color: #339933;">;</span>	
	<span style="color: #000088;">$mResult</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mQuery</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">#Get no of fields in the table
</span>	<span style="color: #000088;">$numFields</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_num_fields</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mResult</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>
&nbsp;
	<span style="color: #666666; font-style: italic;">#Get all fields in the table
</span>	<span style="color: #000088;">$tblFields</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$numFields</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$tblFields</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_field_name</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mResult</span><span style="color: #339933;">,</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">#Store output of fieldnames
</span>	<span style="color: #000088;">$dataStr</span><span style="color: #339933;">.=</span><span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$tblFields</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$dataStr</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">#Store output of all the records
</span>	<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mResult</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$rec</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tblFields</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$tblField</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$recData</span><span style="color: #339933;">=</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$tblField</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$recData</span><span style="color: #339933;">=</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$recData</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$recData</span><span style="color: #339933;">=</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$recData</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000088;">$rec</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$recData</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$dataStr</span><span style="color: #339933;">.=</span><span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$rec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$dataStr</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$dataStr</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$dataStr</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Force the browser to download the file
</span><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: application/octet-stream&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Disposition: attachment; filename=export_<span style="color: #006699; font-weight: bold;">$dbToExport</span>.xls&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Pragma: no-cache&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Expires: 0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$dataStr</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//Display Stored Output</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.nazly.net/export-mysql-database-to-an-ms-excel-format/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Storing Images in MySQL</title>
		<link>http://www.nazly.net/storing-images-in-mysql/</link>
		<comments>http://www.nazly.net/storing-images-in-mysql/#comments</comments>
		<pubDate>Wed, 29 Jun 2005 23:42:30 +0000</pubDate>
		<dc:creator>Nazly</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[store]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://blog.nazly.net/storing-images-in-mysql/</guid>
		<description><![CDATA[I just wrote a quick example that explains how to store images in MySQL database using the BLOB fieldtype which can used to store Binary Data. But I have been used to store the images in the filesystem and have (&#8230;)<p><a href="http://www.nazly.net/storing-images-in-mysql/">Read the rest of this entry &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>I just wrote a quick example that explains how to store images in MySQL database using the BLOB fieldtype which can used to store Binary Data. But I have been used to store the images in the filesystem and have a reference to it in the database. Most of them feel storing images or other binary data in the database is a bad idea as it creates too much overhead. But there are some advantages using this method as well. Have fun.. </p>
<p><b><i>&#8212;&#8211; Table Structure &#8212;&#8211;</i></b><br />
</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;">&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #008000;">`imgtest`</span> <span style="color: #FF00FF;">&#40;</span>
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #008000;">`id`</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">10</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #FF9900; font-weight: bold;">unsigned</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">auto_increment</span><span style="color: #000033;">,</span>
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #008000;">`imgstr`</span> <span style="color: #999900; font-weight: bold;">longblob</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">`id`</span><span style="color: #FF00FF;">&#41;</span>
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><b><i>Insert image to the database</i></b><br />
</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
</pre></td><td 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;">#Image Path
</span><span style="color: #000088;">$imagePath</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/path/to/img/img3.jpg&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Connect to MySQL
</span><span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;******&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Read Image file into a String
</span><span style="color: #000088;">$imgStr</span><span style="color: #339933;">=</span><span style="color: #990000;">addslashes</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$imagePath</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Store Image String to the database
</span><span style="color: #000088;">$query</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;insert into imgtest (imgstr) values ('&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$imgStr</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;')&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</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></td></tr></table></div>

<p><b><i>Display image from database</i></b><br />
</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
</pre></td><td 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;">#Set Content Type
</span><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Type: image/jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Image Id
</span><span style="color: #000088;">$imgId</span><span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>?<span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Connect to MySQL
</span><span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;*****&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Output Image String from database
</span><span style="color: #000088;">$query</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;select imgstr from imgtest where id='<span style="color: #006699; font-weight: bold;">$imgId</span>'&quot;</span><span style="color: #339933;">;</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;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$imgStr</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_result</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;imgstr&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$imgStr</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.nazly.net/storing-images-in-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL 4.1 authentication issue</title>
		<link>http://www.nazly.net/mysql-4-1-authentication-issue/</link>
		<comments>http://www.nazly.net/mysql-4-1-authentication-issue/#comments</comments>
		<pubDate>Sat, 11 Jun 2005 08:30:59 +0000</pubDate>
		<dc:creator>Nazly</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[mysqli]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[phpmyadmin]]></category>

		<guid isPermaLink="false">http://blog.nazly.net/mysql-4-1-authentication-issue/</guid>
		<description><![CDATA[I have been working on MySQL 4.0.17 till now and wanted to upgrad to MySQL 4.1. So I downloaded and installed MySQL 4.1 and the installation was successful and it was working fine. But when I tried to access it (&#8230;)<p><a href="http://www.nazly.net/mysql-4-1-authentication-issue/">Read the rest of this entry &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>I have been working on MySQL 4.0.17 till now and wanted to upgrad to MySQL 4.1. So I downloaded and installed MySQL 4.1 and the installation was successful and it was working fine. But when I tried to access it through PHPMyAdmin or from any of my PHP code it threw this error </p>
<pre><font color="#ff0000">
Client does not support authentication protocol requested by server;
consider upgrading MySQL client
</font></pre>
<p>So I checked it out at MySQL.com to see what I was missing and found out that in MySQL 4.1 the authentication process has been upgraded by making the hashes more secure. So when trying to access it with an old client it throws the above error.</p>
<p>The <a href="http://www.php.net/mysql" target="_blank">mysql extension</a> in PHP doesn&#039;t support the new authentication protocol. So the workaround for this will be to reset the passwords in the format prior to MySQL 4.1. This can be done by using the old_password() function. Make sure to only reset the passwords of the users that need to access using a client prior to 4.1. Here is how it can be done</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;">&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">SET</span> <span style="color: #000099;">PASSWORD</span> FOR
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #008000;">'some<span style="color: #008080; font-weight: bold;">_</span>user'</span>@<span style="color: #008000;">'some<span style="color: #008080; font-weight: bold;">_</span>host'</span> <span style="color: #CC0099;">=</span> <span style="color: #000099;">OLD_PASSWORD</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'newpwd'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>or</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;">&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">UPDATE</span> mysql.<span style="color: #000099;">user</span> <span style="color: #990099; font-weight: bold;">SET</span> <span style="color: #000099;">Password</span> <span style="color: #CC0099;">=</span> <span style="color: #000099;">OLD_PASSWORD</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'newpwd'</span><span style="color: #FF00FF;">&#41;</span>
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #990099; font-weight: bold;">WHERE</span> Host <span style="color: #CC0099;">=</span> <span style="color: #008000;">'some<span style="color: #008080; font-weight: bold;">_</span>host'</span> <span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #000099;">User</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">'some<span style="color: #008080; font-weight: bold;">_</span>user'</span><span style="color: #000033;">;</span>
mysql<span style="color: #CC0099;">&gt;</span> FLUSH <span style="color: #990099; font-weight: bold;">PRIVILEGES</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>The <a href="http://www.php.net/mysqli" target="_blank">mysqli extension</a> (MySQL Improved) which comes with PHP 5 is compatible with the improved password hashing in MySQL 4.1 and above.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nazly.net/mysql-4-1-authentication-issue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

