<?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>Juozas devBlog &#187; MySQL</title>
	<atom:link href="http://dev.juokaz.com/tag/mysql/feed" rel="self" type="application/rss+xml" />
	<link>http://dev.juokaz.com</link>
	<description>Random ideas, scripts and facts</description>
	<lastBuildDate>Mon, 22 Mar 2010 10:48:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Zend Framework and Doctrine. Part 2</title>
		<link>http://dev.juokaz.com/php/zend-framework-and-doctrine-part-2</link>
		<comments>http://dev.juokaz.com/php/zend-framework-and-doctrine-part-2#comments</comments>
		<pubDate>Wed, 18 Nov 2009 10:05:32 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[application.ini]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=866</guid>
		<description><![CDATA[Today we start actual development with Doctrine and Zend Framework. Base of this post is my code which I have been using for quite a few projects and it worked really well.
These are the steps required to setup Doctrine:

Create MySQL (or any other adapter supported by Doctrine) database
Download Doctrine 1.2 (as of today &#8211; 1.2.0beta3). [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://dev.juokaz.com/wp-content/uploads/2009/11/doctrine-orm-php5.png" alt="doctrine-orm-php5" title="doctrine-orm-php5" width="191" height="53" class="size-full wp-image-847" style="float: left; margin-right: 5px;" />Today we start actual development with Doctrine and Zend Framework. Base of this post is my code which I have been using for quite a few projects and it worked really well.</p>
<p>These are the steps required to setup Doctrine:</p>
<ol>
<li>Create MySQL (or any other adapter <a href="http://www.doctrine-project.org/documentation/manual/1_2/en/introduction-to-connections">supported</a> by Doctrine) database</li>
<li>Download Doctrine 1.2 (as of today &#8211; <a href="http://www.doctrine-project.org/download/1_2_0_BETA3/format/tgz/package/">1.2.0beta3</a>). Believe, it&#8217;s stable enough (no problems at all for me) and supports functions all functions we are going to use later</li>
<li>Setup <em>application.ini</em> and <a href="http://framework.zend.com/manual/en/zend.application.quick-start.html#zend.application.quick-start.resources">application resource</a></li>
<li>Generate models from database</li>
<li>Profit!</li>
</ol>
<p>To start with, <strong>download</strong> this <a href="http://dev.juokaz.com/examples/doctrine/doctrine_v1.zip">archive</a> (or get updated version from my public <a href="http://github.com/juokaz/php-examples/tree/master/doctrine-application-resource/">repository</a>). It has everything you will need. Some of the files are quite long so I&#8217;m not going to post them here, it&#8217;s better that you download them and have them ready to be used as the article progresses. </p>
<h5>Create MySQL database</h5>
<p>For MySQL databases I use a product called &#8220;<a href="http://dev.mysql.com/downloads/workbench/5.1.html">MySQL Workbench</a>&#8220;. If you haven&#8217;t tried it I definitely recommend to give it a go &#8211; it basically allows you create a database as a <a href="http://forge.mysql.com/w/images/a/a8/Mysql_workbench_tbl_editor.png">diagram</a> and then export it to sql file or update actual database (can be risky). Since I expect you to have enough knowledge how to create a database I won&#8217;t write anything more &#8211; you can find <a href="http://www.webpronews.com/topnews/2005/06/06/mysql-for-beginners-how-to-create-a-mysql-database">tutorials</a> all over the web.</p>
<h5>Download Doctrine</h5>
<p>After downloading Doctrine extract it to <em>library</em> folder. You should have <em>./library/Doctrine.php</em> in your library folder. Although, you can use <em>svn:externals</em> (if you are using SVN at all) to remove Doctrine code from repository, but in this case you probably need to setup paths in application resource file explain below to use Doctrine_Core if you just get Doctrine folder contents (from <a href="http://svn.doctrine-project.org/tags/1.2.0-BETA3/lib/Doctrine/">here</a>).</p>
<p>Most important point here &#8211; don&#8217;t forget to <a href="http://www.doctrine-project.org/download">download</a> <strong>1.2</strong>, not version 1.1. Even though it doesn&#8217;t have a stable release, 1.1 doesn&#8217;t support models generation as we want them to be (auto-loadable). Also this week all bugs have been fixed which I reported and now models generation works perfectly. From my personal experience, I haven&#8217;t even used 1.1 at all (started with 1.2 when it was in alpha) and didn&#8217;t had any problems, so my recommendation &#8211; use 1.2. </p>
<h5>Setup application.ini and application resource</h5>
<p>From the file listed above open a file called <em>application.ini</em>. Append you current configuration (more on getting Zend Framework project running can be found <a href="http://framework.zend.com/docs/quickstart/create-your-project">here</a>) with settings in that file (leave compiled and cache options to false for now). Doctrine uses DNS strings to connect to a database, there are quite a few <a href="http://www.doctrine-project.org/documentation/manual/1_2/en/introduction-to-connections#dsn,-the-data-source-name:examples">examples</a> in the documentation. If you have used <a href="http://php.net/manual/en/book.pdo.php">PDO</a> before you be very familiar.</p>
<p>In archive there is a file called <em>library/resource.php</em>. Depending on what namespace you use for outside-Zend code, paste it to chosen folder. If you choose to have &#8220;App&#8221; as a namespace, just copy that file to <em>library/App/Application/Resource/Doctrine.php</em>. And don&#8217;t forget to have:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">autoloaderNamespaces<span style="">&#91;</span><span style="">&#93;</span> <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;App_&quot;</span>
pluginpaths.App_Application_Resource <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;App/Application/Resource&quot;</span></pre></div></div>

<p>in <em>application.ini</em> also, otherwise library code won&#8217;t be auto-loaded. I don&#8217;t recommend putting any code in Zend folder, because it will create tons of problems in the future. Having App or ProjectName library folders allows to have your own code in separate packages which you can use in later projects (I usually have App, ProjectName, CompanyName). </p>
<p>Provided <em>application.ini</em> file isn&#8217;t that much configurable, mainly because it wasn&#8217;t supposed to be released at all. If you are looking for something more dynamic, look no further than this <a href="http://framework.zend.com/wiki/display/ZFPROP/Zend_Application_Resource_Doctrine+-+Matthew+Lurz">proposal</a> in Zend incubator. Nevertheless, my code should work fine &#8211; it has all options required to get you started and have been tweaked with settings which I found to be really useful. </p>
<h5>Generate models from database</h5>
<p>This part is the most awesome. To start, just copy and paste everything from <em>scripts</em> folder in the zip archive to your scripts folder in application (I have scripts folder in the same level as application and library, in the root of project). There actually only two files &#8211; <em>doctrine-cli.php</em> and custom task in <em>Doctrine/Task/GenerateModels.php</em>. You can run doctrine-cli.php like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">php doctrine-cli.php</pre></div></div>

<p>and you should get a list of <a href="http://www.doctrine-project.org/documentation/manual/1_2/en/utilities#command-line-interface">all the possible tasks</a> (if you setup everything properly). To run my custom task, append &#8220;generate-models&#8221; to the end of previous command to get:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">php doctrine-cli.php generate-models</pre></div></div>

<p>This task will load your database schema and create all classes (table, base model, model) required for models. Doctrine also supports generating models from <em><a href="http://en.wikipedia.org/wiki/YAML">yaml</a></em> configuration files, though I&#8217;ve never used it before, but configuration should be almost the same.</p>
<h5>It works</h5>
<p>To test the models you can look at the generate code or start coding you application (or even run <em><a href="http://www.doctrine-project.org/documentation/manual/1_2/en/dql-doctrine-query-language">dql</a></em> task from <em>doctrine-cli.php</em>). For example you can test simply like this (adjust by models you have):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$product</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Model_Product<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Product name'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span>Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Model_Product'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If everything has been configured properly you shouldn&#8217;t get any errors and this code (put it in controller) should output array of your saved data. Because of the settings in application resource, save() method also runs <a href="http://www.doctrine-project.org/documentation/manual/1_2/en/data-validation">validation</a> so if you missed a field and it breaks a <em>not null</em> constrain, this code will throw a validator exception. </p>
<h5>What&#8217;s next?</h5>
<p>This article has enough information to get you started and almost all the code provided it left as simple as possible and open for further modifications. I hope that in coming months code for Doctrine and Zend Framework integration will be completed and you can use Doctrine without any outside code.</p>
<p>Since we have application running now we can dive into actual usage, testing and more advanced stuff. I have code for these also and will be posting more articles very soon, even though the best resource for Doctrine is <a href="http://www.doctrine-project.org/documentation/manual/1_2/en/introduction">documentation</a>. If you have any problems with this code just leave a comment here or find my on twitter (<a href="http://www.twitter.com/juokaz">here</a>) and I will try to explain more.</p>
<p><strong>All parts</strong>:</p>
<ol>
<li><a href="http://dev.juokaz.com/php/zend-framework-and-doctrine-part-1">Zend Framework and Doctrine. Part 1</a></li>
<li><a href="http://dev.juokaz.com/php/zend-framework-and-doctrine-part-2">Zend Framework and Doctrine. Part 2</a></li>
<li><a href="http://dev.juokaz.com/php/zend-framework-and-doctrine-part-3">Zend Framework and Doctrine. Part 3</a></li>
</ol>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/php/zend-framework-and-doctrine-part-2/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Zend Framework and Microsoft IIS</title>
		<link>http://dev.juokaz.com/winphp-2009/zend-framework-and-microsoft-iis</link>
		<comments>http://dev.juokaz.com/winphp-2009/zend-framework-and-microsoft-iis#comments</comments>
		<pubDate>Tue, 05 May 2009 01:52:52 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[WinPhp 2009]]></category>
		<category><![CDATA[driver]]></category>
		<category><![CDATA[iis]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[permissions]]></category>
		<category><![CDATA[rob allen]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[windows 7]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=617</guid>
		<description><![CDATA[My task today was to make Rob Allen&#8217;s Zend framework tutorial project run on Windows. Some years ago such task could have been a problem, but as you will see, now it&#8217;s nothing special.
To start with, Windows sometimes really annoys. For example, by default I can&#8217;t add files to C:\inetpub\wwwroot. It doesn&#8217;t ask for &#8220;Allow/Cancel?&#8220;, [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-460" title="Zend framework" src="http://dev.juokaz.com/wp-content/uploads/2009/04/logo-zend-framework.jpg" alt="Zend framework" width="115" height="77" />My task today was to make Rob Allen&#8217;s <a href="http://akrabat.com/zend-framework-tutorial/">Zend framework tutorial</a> project run on Windows. Some years ago such task could have been a problem, but as you will see, now it&#8217;s nothing special.</p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/05/iis-2.png"><img class="alignnone size-thumbnail wp-image-622" style="margin-left: 5px; float: right;" title="Access is denied" src="http://dev.juokaz.com/wp-content/uploads/2009/05/iis-2-150x150.png" alt="Access is denied" width="100" height="100" /></a>To start with, Windows sometimes really annoys. For example, by default I can&#8217;t add files to C:\inetpub\wwwroot. It doesn&#8217;t ask for &#8220;<a href="http://www.youtube.com/watch?v=FxOIebkmrqs">Allow/Cancel?</a>&#8220;, is doesn&#8217;t show permissions error &#8211; you just can&#8217;t do anything. I decided not to waste my time trying to find a way how to do it and ended up with virtual directory pointed to my project folder. I&#8217;m using Windows 7, so one can run programs as administrator, but having two Explorers is not a very good idea, especially when you can&#8217;t tell a difference between admin and non-admin mode (can you?).</p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/05/iis-3.png"></a><a href="http://dev.juokaz.com/wp-content/uploads/2009/05/iis-3.png"><img class="alignnone size-thumbnail wp-image-618" style="float: right; margin-left: 5px;" title="Import .htaccess" src="http://dev.juokaz.com/wp-content/uploads/2009/05/iis-3-150x150.png" alt="Import .htaccess" width="100" height="100" /></a>First problem was <a href="http://framework.zend.com/manual/en/zend.controller.router.html">routes</a> &#8211; almost all frameworks nowadays uses something like &#8220;/controller/action/id&#8221; which is achieved by <a href="http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html">mod_rewrite</a> and <a href="http://httpd.apache.org/docs/1.3/howto/htaccess.html">.htaccess</a>. However, IIS is not Apache, hence .htaccess doesn&#8217;t exist. Although IIS has a routing module which pretty much works the same and also has nice a GUI for all who doesn&#8217;t know how to use <a href="http://en.wikipedia.org/wiki/Regular_expression">regexp</a>&#8217;s. Furthermore, it also has import function which surprisingly imports from .htaccess &#8211; select file, click Import, remove or fix not recognized rules and click Apply. I haven&#8217;t tried any complex rules, but since most of frameworks only require to redirect everything (&#8220;^.*$&#8221;) it should work.</p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/05/mssql-1.png"><img class="alignnone size-thumbnail wp-image-632" style="float: right; margin-left: 5px;" title="Microsoft SQL" src="http://dev.juokaz.com/wp-content/uploads/2009/05/mssql-1-150x150.png" alt="Microsoft SQL" width="100" height="100" /></a>Next &#8220;problem&#8221; is the database. MySQL works fine in Windows, but I wanted to use <a href="http://en.wikipedia.org/wiki/Microsoft_SQL_Server">MSSQL</a>. Not only because I haven&#8217;t tried MSSQL before, but also because MSSQL is much more integrated into Windows. However, MSSQL driver in PHP has some <a href="http://blog.stuartherbert.com/php/2007/10/16/microsofts-first-php-extension-sql-server-2005-support/">limitations</a>. Nevertheless, Microsoft has released a <a href="http://msdn.microsoft.com/en-us/data/aa937733.aspx">native driver</a>, which seems to have fixed all previous problems. You only need to download actual driver from <a href="http://download.microsoft.com/download/2/7/c/27c60d49-6dbe-423e-9a9e-1c873f269484/sqlncli.msi">here</a> and PHP extensions from <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=61BF87E0-D031-466B-B09A-6597C21A2E2A&amp;displaylang=en">here</a>. Extension can be used like <a href="http://akrabat.com/2009/05/04/mssql-and-php/">this</a> (it provides <a href="http://msdn.microsoft.com/en-us/library/cc296152(SQL.90).aspx">sqlsrv_*</a> functions), however I really prefer using <a href="http://uk3.php.net/manual/en/intro.pdo.php">PDO</a> or other object oriented database layer. That&#8217;s why I use it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$hostname</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;localhost\SQLEXPRESS&quot;</span><span style="color: #339933;">;</span>            <span style="color: #666666; font-style: italic;">//host</span>
    <span style="color: #000088;">$dbname</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;zf-tutorial&quot;</span><span style="color: #339933;">;</span>            <span style="color: #666666; font-style: italic;">//db name</span>
    <span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;sa&quot;</span><span style="color: #339933;">;</span>            <span style="color: #666666; font-style: italic;">// username like 'sa'</span>
    <span style="color: #000088;">$pw</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;juozas&quot;</span><span style="color: #339933;">;</span>                <span style="color: #666666; font-style: italic;">// password for the user</span>
&nbsp;
    <span style="color: #000088;">$dsn</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Driver={SQL Server Native Client 10.0};
            Server=<span style="color: #006699; font-weight: bold;">{$hostname}</span>;
            Database=<span style="color: #006699; font-weight: bold;">{$dbname}</span>;
            Uid=<span style="color: #006699; font-weight: bold;">{$username}</span>;
            Pwd=<span style="color: #006699; font-weight: bold;">{$pw}</span>;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$dbh</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PDO <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;odbc:&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$dsn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>PDOException <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Failed to get DB handle: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> PHP_EOL<span style="color: #339933;">;</span>
    <span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dbh</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 albums&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I should add, that I haven&#8217;t tested it much, but at least demo project from the tutorial works. Later this week I will probably choose which method to use (sqlsrv_* or PDO), because Zend framework currently doesn&#8217;t support <a href="http://msdn.microsoft.com/en-us/data/aa937733.aspx">MSSQL Native client</a> and I will implement adapter myself (Zend_Db_Adapters are around 500 lines). <a href="http://akrabat.com/">Rob Allen</a> is also working on a driver implementation for Zend so one of us should come with a solution pretty soon. Currently I&#8217;m using Zend_Db_Adapter_Pdo_Odbc from <a href="http://framework.zend.com/issues/browse/ZF-905">here</a>, however it still works only as a hack.</p>
<p>That&#8217;s all &#8211; your Zend Framework project should work (you may also need to set correct <a href="http://drupal.org/node/202491">file permissions</a>).</p>
<p><em>If I wouldn&#8217;t have started <a href="http://wiki.phpconference.nl/2009_WinPHP_Challenge">Winphp</a> competition, I may have never realized how Microsoft is struggling to support PHP. Starting from <a href="http://blogs.msdn.com/sqlphp/">blogs</a> and ending with special <a href="http://www.codeplex.com/SQL2K5PHP">drivers</a>, <a href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;FamilyID=c8498c9b-a85a-4afa-90c0-593d0e4850cb">tutorials</a> and even <a href="http://www.phponwindows.ca/ftw/">competitions</a>. Is Microsoft trying to &#8220;repair&#8221; its public profile?</em></p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/winphp-2009/zend-framework-and-microsoft-iis/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Setting up Windows for PHP server</title>
		<link>http://dev.juokaz.com/winphp-2009/setting-up-windows-for-php-server</link>
		<comments>http://dev.juokaz.com/winphp-2009/setting-up-windows-for-php-server#comments</comments>
		<pubDate>Tue, 21 Apr 2009 11:37:35 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[WinPhp 2009]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[iis]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[skype]]></category>
		<category><![CDATA[virtualbox]]></category>
		<category><![CDATA[web platform]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[windows server]]></category>
		<category><![CDATA[winphp]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=469</guid>
		<description><![CDATA[To start with, all work took about 1:30, including Skype distractions, reading how-to&#8217;s, etc. Only one problem I had is that I was given remote desktop connection access to Windows Server 2008 machine, but I&#8217;m working with Linux. How to connect to remote desktop from Linux?
Luckily, I had Windows XP in VirtualBox so I tried [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-493" style="float: left; margin-right: 5px;" title="iis_logo" src="http://dev.juokaz.com/wp-content/uploads/2009/04/iis_logo.png" alt="iis_logo" width="160" height="76" />To start with, all work took about 1:30, including <a href="http://www.skype.com">Skype</a> distractions, reading how-to&#8217;s, etc. Only one problem I had is that I was given <a href="http://www.microsoft.com/windowsXp/using/mobility/getstarted/Remoteintro.mspx">remote desktop</a> connection access to Windows Server 2008 machine, but I&#8217;m working with Linux. How to connect to remote desktop from Linux?</p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-15.png"><img class="alignnone size-thumbnail wp-image-488" style="float: right; margin-left: 5px;" title="screenshot-windows-xp-running-sun-xvm-virtualbox-15" src="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-15-150x150.png" alt="screenshot-windows-xp-running-sun-xvm-virtualbox-15" width="100" height="100" /></a>Luckily, I had Windows XP in <strong><a href="http://www.virtualbox.org/">VirtualBox</a></strong> so I tried it. And it worked really well. I&#8217;m using <a href="http://www.ed.ac.uk">university</a> internet network so connections speeds are very fast and Ubuntu -&gt; Windows Xp -&gt; Windows Server 2008 worked just fine. Later I will probably use real Windows system, but for first steps virtual machine worked much more faster than I expected.</p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-3.png"><img class="size-thumbnail wp-image-475" style="float: right; margin-left: 5px;" title="screenshot-windows-xp-running-sun-xvm-virtualbox-3" src="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-3-150x150.png" alt="screenshot-windows-xp-running-sun-xvm-virtualbox-3" width="100" height="99" /></a> Almost 4 years ago I had experience installing <strong><a href="http://en.wikipedia.org/wiki/Internet_Information_Services">IIS</a></strong> server, but I haven&#8217;t done much since then. Nevertheless, installing IIS+PHP was really easy &#8211; I followed <a href="http://blogs.msdn.com/bramveen/archive/2009/04/20/creating-your-php-dev-environment-for-the-winphp-challenge.aspx">this</a> tutorial, which shows you how to install <a href="http://www.microsoft.com/web/platform/default.aspx">Microsoft Web Platform</a>, and IIS was ready. Platform installer allows you to install not only SQL, PHP, ASP.NET, etc. but also various web applications &#8211; <a href="http://www.wordpress.org">Wordpress</a>, <a href="http://drupal.org/">Drupal</a> and many more. Experience with IIS was very similar to <a href="http://dev.juokaz.com/php/zend-server-complete-php-environment-in-minutes">Zend Server</a>, but IIS control panel is way better.</p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-8.png"><img class="size-thumbnail wp-image-478" style="float: right; margin-left: 5px;" title="screenshot-windows-xp-running-sun-xvm-virtualbox-8" src="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-8-150x150.png" alt="screenshot-windows-xp-running-sun-xvm-virtualbox-8" width="100" height="100" /></a>I chose to install Wordpress, just to test if IIS is working properly, but Platform installer doesn&#8217;t install <strong>MySQL</strong>. PHP is installed with MySQL extension, but no server is being installed &#8211; quite illogical. However, installing MySQL server is as easy as installing any other windows application (subjective) &#8211; download installer, Next, Next, Next and server is running. Also, <em>mysql</em> tool works the same in Windows <a href="http://en.wikipedia.org/wiki/Windows_command_line">cmd</a> and Linux console.</p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-10.png"><img class="alignnone size-thumbnail wp-image-491" style="float: right; margin-left: 5px;" title="screenshot-windows-xp-running-sun-xvm-virtualbox-10" src="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-10-150x150.png" alt="screenshot-windows-xp-running-sun-xvm-virtualbox-10" width="100" height="100" /></a>Tutorials and other information:</p>
<ul>
<li> <a href="http://blogs.msdn.com/bramveen/archive/2009/04/20/creating-your-php-dev-environment-for-the-winphp-challenge.aspx">Creating your PHP dev environment for the WinPHP Challenge </a></li>
<li> <a href="http://www.microsoft.com/web/downloads/platform.aspx">Microsoft Web Platform</a></li>
<li><a href="http://learn.iis.net/page.aspx/610/setting-up-mysql-for-php-applications/">Setting up MySQL for PHP applications</a></li>
<li><a href="http://php.iis.net/">PHP on IIS7</a></li>
</ul>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-14.png"><img class="alignnone size-thumbnail wp-image-485" style="float: right; margin-left: 5px;" title="screenshot-windows-xp-running-sun-xvm-virtualbox-14" src="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-14-150x150.png" alt="screenshot-windows-xp-running-sun-xvm-virtualbox-14" width="101" height="101" /></a><br />
What I liked very much is GUI for web server control.  Currently I have 13 web apps running in my laptop and every app has different port (localhost:80, localhost:81, localhost:82, etc.), but I haven&#8217;t found any good tool for adding more sites. I would like to have simple tool, which would accept port number and directory, and create virtual host. IIS control application allows it very easily.</p>
<p>In conclusion, working with PHP in Windows server seems to be pretty acceptable. I haven&#8217;t tested setting up anything except Wordpress, but Wordpress seems to work just fine. Especially I liked using GUI, because most of tasks for development machine can be done much more easier and faster, still I&#8217;m not going to switch from Linux yet.</p>
<p><strong>Advantages</strong>:</p>
<ul>
<li>GUI</li>
<li>Easy to setup</li>
<li>PHP works</li>
</ul>
<p><strong>Disadvantages</strong>:</p>
<ul>
<li>Some functionality is hard to find</li>
<li>Differences from Apache (<a href="http://www.petefreitag.com/item/286.cfm">mod_rewrite</a>, etc.)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/winphp-2009/setting-up-windows-for-php-server/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Starting with Zend_Search_Lucene</title>
		<link>http://dev.juokaz.com/php/starting-with-zend_search_lucene</link>
		<comments>http://dev.juokaz.com/php/starting-with-zend_search_lucene#comments</comments>
		<pubDate>Wed, 11 Mar 2009 14:19:37 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[full-text]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[lucene]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[speed]]></category>
		<category><![CDATA[sphinx]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=344</guid>
		<description><![CDATA[As websites grows, searches like &#8220;LIKE title &#8216;%search term%&#8217;&#8221; becomes unreliable. There are very good solutions like Sphinx, Lucene, etc, but not surprisingly, you can&#8217;t always have Sphinx installed (shared servers again) and other solutions should be chosen. 
MySQL supports full-text indexing, but it doesn&#8217;t give a lot of control over actual index. Luckily, Zend [...]]]></description>
			<content:encoded><![CDATA[<p>As websites grows, searches like &#8220;LIKE title &#8216;%search term%&#8217;&#8221; becomes unreliable. There are very good solutions like <a href="http://sphinxsearch.com/">Sphinx</a>, <a href="http://lucene.apache.org/java/docs/">Lucene</a>, etc, but not surprisingly, you can&#8217;t always have Sphinx installed (shared servers <a href="http://dev.juokaz.com/php/using-phing-to-synchronize-files">again</a>) and other solutions should be chosen. </p>
<p>MySQL supports <a href="http://www.devarticles.com/c/a/MySQL/Getting-Started-With-MySQLs-Full-Text-Search-Capabilities/">full-text indexing</a>, but it doesn&#8217;t give a lot of control over actual index. Luckily, Zend team has done wonderful job and implemented Lucene search in PHP (100%). <a href="http://framework.zend.com/manual/en/zend.search.lucene.html">Zend_Search_Lucene</a> is part of Zend Framework, but as all framework modules runs almost independently (it uses Zend_Exception, etc.).</p>
<p>How you start indexing data? Zend <a href="http://framework.zend.com/manual/en/zend.search.lucene.index-creation.html">manual</a> has very good examples how to start with Lucene, but to create sample index index you can use this code (you need to have auto-loading enabled and db connection available):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Create index</span>
<span style="color: #000088;">$index</span> <span style="color: #339933;">=</span> Zend_Search_Lucene<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'indexes/products'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;select product_name, product_url from products&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchAll</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$doc</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Search_Lucene_Document<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Store document URL to identify it in the search results</span>
    <span style="color: #000088;">$doc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addField</span><span style="color: #009900;">&#40;</span>
    Zend_Search_Lucene_Field<span style="color: #339933;">::</span><span style="color: #004000;">UnIndexed</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'url'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">product_url</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;">// Index document title</span>
    <span style="color: #000088;">$doc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addField</span><span style="color: #009900;">&#40;</span>
    Zend_Search_Lucene_Field<span style="color: #339933;">::</span><span style="color: #004000;">Text</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">product_name</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;">// Add document to the index</span>
    <span style="color: #000088;">$index</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDocument</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$doc</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;">// Optimize index.</span>
<span style="color: #000088;">$index</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">optimize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This simple code will select products information from database, loop through results and add them as documents to index. In this example I added <em>url</em> as UnIndexed, because I&#8217;m only going to search by title, but Lucene allows other <a href="http://framework.zend.com/manual/en/zend.search.lucene.html#zend.search.lucene.index-creation.understanding-field-types">field types</a>. In most cases, product description or document text should be added (or maybe even indexed).</p>
<p>Searching through index is even easier. One thing you need to learn is how to construct search queries in required <a href="http://framework.zend.com/manual/en/zend.search.lucene.query-language.html">query language</a>. Example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Open index</span>
<span style="color: #000088;">$index</span> <span style="color: #339933;">=</span> Zend_Search_Lucene<span style="color: #339933;">::</span><span style="color: #004000;">open</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'indexes/products'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'title:&quot;Apple MacBook&quot;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Search by query</span>
<span style="color: #000088;">$hits</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$index</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$hits</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$hit</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$hit</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">score</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$hit</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$hit</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span> <span style="color: #339933;">.</span> PHP_EOL<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I tried creating index of 6&#8242;000 products, index (0.7 MB) was created in around 3 minutes and all searches takes about 0.1 s. I tested it on my laptop, without APC and with development Apache/PHP configuration. Normal servers would run this task much more faster, but 0.1 for search is not that bad.</p>
<p>Zend_Search_Lucene will not change Sphinx or Lucene, but in limited environments (like shared servers) it can be quite useful. It supports many query types: phrase queries, boolean queries, wildcard queries, proximity queries, range queries and many other, what can be hardly achieved with using full-text MySQL indexes.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/php/starting-with-zend_search_lucene/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Zend Server &#8211; complete PHP environment in minutes</title>
		<link>http://dev.juokaz.com/php/zend-server-complete-php-environment-in-minutes</link>
		<comments>http://dev.juokaz.com/php/zend-server-complete-php-environment-in-minutes#comments</comments>
		<pubDate>Thu, 19 Feb 2009 17:06:59 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Servers]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[environment]]></category>
		<category><![CDATA[installation]]></category>
		<category><![CDATA[localhost]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[review]]></category>
		<category><![CDATA[tests]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[zend server]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=189</guid>
		<description><![CDATA[I believe that everyone who develops in PHP knows Zend (I know them from certification which I&#8217;ve done some time ago), especially now, when Zend Framework is more and more popular. Zend also makes (and probably specializes) software for web servers &#8211; Zend Guard, Zend Optimizer, etc. Recently Zend introduced new product &#8211; Zend Server. [...]]]></description>
			<content:encoded><![CDATA[<p>I believe that everyone who develops in <a href="http://www.php.net">PHP</a> knows <a href="http://www.zend.com">Zend</a> (I know them from certification which I&#8217;ve done some time ago), especially now, when <a href="http://framework.zend.com/">Zend Framework</a> is more and more popular. Zend also makes (and probably specializes) software for web servers &#8211; Zend Guard, Zend Optimizer, etc. Recently Zend introduced new product &#8211; <a href="http://www.zend.com/en/products/server/">Zend Server</a>. They introduce it by:</p>
<blockquote><p>Zend Server is a complete, enterprise-ready Web Application Server for running and managing PHP applications that require a high level of reliability, performance and security.</p></blockquote>
<p>I haven&#8217;t tried it &#8211; probably it will be commercial solution (Zend Studio == <a href="http://www.zend.com/en/products/platform/">Zend Platform</a>?) and I&#8217;m not looking for one right one. So I jumped over to <a href="http://www.zend.com/en/community/zend-server-ce">Zend Server CE</a> (Community Edition) which is:</p>
<blockquote><p>The free community edition of Zend Server enables you to set up a complete PHP environment in minutes, and run non-critical PHP applications in development, testing and production</p></blockquote>
<p>Main <a href="http://www.zend.com/en/products/server/editions">differences</a> lie deep inside PHP configuration &#8211; not-community version is much more tweaked for performance and production servers, where community edition is for development environments.</p>
<p>To start with, installing Zend Server is a joke and can be done in no-time. Download one file from <a href="http://www.zend.com/en/products/server/downloads-all">here</a>, extract it and run in terminal. Thats it! After installation web server was accessible on localhost:10088 and web-based configuration on localhost:10082/ZendServer. Everything worked smoothly and without any errors.</p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/02/screenshot-juokazthinkpad-desktop-zendserver-400beta1ms16rc8-linux-glibc23-i386.png"><img class="alignnone size-full wp-image-190" title="Zend Server installation" src="http://dev.juokaz.com/wp-content/uploads/2009/02/screenshot-juokazthinkpad-desktop-zendserver-400beta1ms16rc8-linux-glibc23-i386.png" alt="Zend Server installation" width="500" height="376" /></a></p>
<p>Web interface gives access to server logs, PHP parameters (allows  you to change them, restart server), extensions, debugger, Zend products configuration (Optimizer, Java Bridge, etc.) and some more. If they just add Apache VirtualHosts configuration it would be perfect for me, because I&#8217;m still doing it by hand.</p>
<p>Performance is subjective and can&#8217;t be judged fast, so I&#8217;m not going to compare this package to standalone servers. Probably it will run faster or at least as fast as default configuration Apache+PHP. I do believe that Zend made some configuration improvements, but you only need them if you can&#8217;t (or are to lazy) do them yourself.</p>
<p>I have been using standard <a href="http://httpd.apache.org/">Apache</a>, PHP, MySQL servers for ages, and haven&#8217;t really ever tried anything like this. It&#8217;s really user friendly, gives a lot of power through simple interface, and what&#8217;s most important is very easy to setup. Probably Xampp (or just running <em>sudo apt-get install apache2 php5</em>) can be installable as fast as Zend Server, but in my belief, Zend Server is much more production-like environment. Depends what you need.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/php/zend-server-complete-php-environment-in-minutes/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Solving Sudoku puzzle with only MySQL</title>
		<link>http://dev.juokaz.com/mysql/solving-soduku-with-only-mysql</link>
		<comments>http://dev.juokaz.com/mysql/solving-soduku-with-only-mysql#comments</comments>
		<pubDate>Wed, 11 Feb 2009 15:35:45 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[combinations]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[geeks]]></category>
		<category><![CDATA[lamneth]]></category>
		<category><![CDATA[positions]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[solver]]></category>
		<category><![CDATA[sudoku]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=84</guid>
		<description><![CDATA[More than a year ago I&#8217;ve read very interesting article in Lamneth blog about MySQL Sudoku solver. I&#8217;ve searched for some time, but information was very minimal and I couldn&#8217;t find a way to contact original author. So I decided to try it myself and easily found working solution.
Beauty of solving Sudoku puzzle in MySQL [...]]]></description>
			<content:encoded><![CDATA[<p>More than a year ago I&#8217;ve read very interesting <a href="http://lamneth.wordpress.com/2007/07/26/can-you-solve-a-sudoku-puzzle-faster-than-this/">article</a> in Lamneth blog about MySQL Sudoku solver. I&#8217;ve searched for some time, but information was very minimal and I couldn&#8217;t find a way to contact original author. So I decided to try it myself and easily found working solution.</p>
<p>Beauty of solving Sudoku puzzle in MySQL is that you don&#8217;t need any server-side code (almost) at all. Only thing you need to do is to form a query and then pass it to MySQL server. Tasks list for 9&#215;9 Sudoku puzzle would like this:</p>
<ol>
<li>Table will all possible combinations (362880, 9*8*7*6*5*4*3*2*1)</li>
<li>Query to:
<ul>
<li>join combinations table 9 times</li>
<li>set already known cell values</li>
<li>get results</li>
</ul>
</li>
</ol>
<p>It may seem not very clear, but it&#8217;s very trivial method. <em>Positions</em> table holds all possible combinations, so you select 9 rows from this table, where each cell is unique in row and column and some predefined cells equal to given values. Basically, you tell MySQL <a href="http://www.sudoku.name/rules/en">Sudoku rules</a> and MySQL does all work for you.</p>
<p style="text-align: center;"><img class="size-full wp-image-91 aligncenter" style="margin-right: 40px;" title="Solving" src="http://dev.juokaz.com/wp-content/uploads/2009/02/solving.png" alt="Solving" width="459" height="345" /></p>
<p>I&#8217;ve published sample query and positions table data online here: <a href="http://dev.juokaz.com/examples/sudoku/query.sql.gz">positions</a>, <a href="http://dev.juokaz.com/examples/sudoku/run.sql">query</a>. Example will solve puzzle given in original Lamneth post (<a href="http://polly97.wordpress.com/2007/06/13/sudoku/">here</a>) and it shouldn&#8217;t take too long &#8211; in my laptop it takes about 0.7 s. I haven&#8217;t tried any optimizations, but I strongly believe that solving process can be optimized dramatically (especially for <em>hard</em> puzzles).</p>
<p>This method works pretty good &#8211; almost all puzzles are solved around 1 s., and only hard ones takes a while. Compared to real solvers it&#8217;s very slow, but since it&#8217;s only a simple query &#8211; results look very good.</p>
<p>MySQL is not made for games, but for fun you can make some nice applications (solving Sudoku with MySQL will not help you to get a better job). How much faster you can make this query run?</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/mysql/solving-soduku-with-only-mysql/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
