A blog by Iwan Luijks, an unofficial Microsoft evangelist talking about PHP, .NET, other programming languages and information theory.

Connecting to MySQL with R using RODBC on Windows XP

Posted on by in statistics

introduction

Every now and then I want to gather some specific information from my databases, e.g. what the probability is of new visitors showing up during a certain season. This could be for example both interesting for determining upcoming serverload and bandwidth usage as well as other things.

For gathering this information I use R, a very powerful language for doing all sorts of analysis on all sorts of data. It is besides my use case for example also used for analysis of genomic data.

In this blog post I'll keep it simple, and give you a kickstart by showing you how to connect to a MySQL-server via ODBC with R. When connected, you can start analysing your data, this however is something I don't cover in this post.

ODBC?

Yep ODBC.. Normally I would use the RMySQL package for connecting to a MySQL-server, but since I wanted to do my analysis on an old laptop lying around running Windows XP and no RMySQL package was available in CRAN for my version of R (2.15.2), RODBC was the quickest one to setup.

For this being cleared up, let's give it a go.

R

Before setting everything else up we first install R. R for Windows can be downloaded from http://cran.r-project.org/bin/windows/base/, the version I use is 2.15.2. Follow the installation instructions, choosing for a "Full installation" and a customized startup at the "Startup options" screen to setup your preferred way of display, for me that was: "MDI", "HTML help" and "Internet2" while having "C:\Program Files\R\R-2.15.2" chosen as installation directory.

That was it, now let's install the RODBC package..

RODBC

Having installed R, we can launch RGui which is shipped with R. Depending on your installation preferences, it can be found on your Desktop, Start menu, and as "C:\Program Files\R\R-2.15.2\bin\i386\Rgui.exe".

For simplicity of installing packages in the future, I recommend selecting a CRAN mirror beforehand. You can do this be entering the following in the console RGui opens up by default:

chooseCRANmirror()

This launches a large list of available mirrors. Select the one you want (mine is "Utrecht University") and hit "OK".

Now for installing the RODBC package, enter the command:

utils:::menuInstallPkgs()

Scroll down and select "RODBC", finish by clicking "OK". The following should now show up in the console:

trying URL 'http://cran-mirror.cs.uu.nl/bin/windows/contrib/2.15/RODBC_1.3-6.zip'
Content type 'application/zip' length 759673 bytes (741 Kb)
opened URL
downloaded 741 Kb

package ‘RODBC’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
        C:\Documents and Settings\Iwan Luijks\Local Settings\Temp\RtmpmW37k7\downloaded_packages

Now you have RODBC installed. Let's configure a MySQL data source we can use.

MySQL Connector/ODBC

Before can configure our MySQL data source however, we need the have the MySQL/Connector ODBC installed so we can actually connect to a MySQL-server via ODBC.

Go to http://dev.mysql.com/downloads/connector/odbc/ and start your download (I used the MSI-installer, version 5.2.4-ansi). The installation is pretty straight-forward so I won't dive into that one.

Adding the data source

Windows has many shortcuts for accessing the Control Panel Files, for configuring ODBC data sources there is "Odbccp32.cpl". So press "Windows+R" and enter "Odbccp32.cpl" in the input field followed by clicking "OK". This launches the "ODBC Data Source Administrator". Being in the "ODBC Data Source Administrator", the default tab opened is "User DSN" where DSN stands for Data Source Name used for describing the connection to a Data Source. The name of the Data Source is used as first parameter when setting up a connection using RODBC, so remember the one you enter next.

Hit "Add..." and select "MySQL ODBC 5.2a Driver". Click "Finish" to open the "MySQL Connector/ODBC Data Source Configuration" dialog. Enter the credentials for connecting to your MySQL-server and optionally hit the "Test"-button for testing the connection.

Note that you might want to set additional connection options, these can be found under the "Details >>"-button.

When you're done click "OK" and close the "ODBC Data Source Administrator" dialog.

Back to RGui

Find your way back to RGui and in the console enter the following:

library(RODBC)
ch <- odbcConnect("THE_DSN_YOU_ENTERED_BEFORE")
sqlTables(ch)
odbcClose(ch)

Where these functions above do the following (line-by-line):

  1. load the RODBC library
  2. connect to your Data Source using the DSN you specified before and assign that connection to the variable "ch"
  3. list all tables
  4. close the connection you setup using odbcConnect()

That's actually all.. Now go analyse your data! ☺

  • R
  • RODBC
  • MySQL
  • Windows

E-mailing with PHP on Windows using Apache James

Posted on by in php

introduction

In opposite to my usual posts, like the one coming up on MySQL, PHP, SSL and that sort of stuff I decided to keep this post short.. real short. Read on to learn how easy it is setup an e-mail environment on Windows ready to use from PHP using the Apache James Server.

Note that this post is specifically targeted to setting up a simple e-mailserver usable in development environments.

Apache James

There are a lot of useful Apache projects out there, and though I am an (unofficial) Microsoft evangalist, I find some of them even nifty enough to use. One of these is the Apache James Server.

The Apache James Project site provides us with a short explanation of what it represents: The Apache James Project delivers a rich set of open source modules and libraries, written in Java, related to Internet mail communication which build into an advanced enterprise mail server..

Yep.. Java.. not all that bad, I personally even like Java, and you should too for now. Given this:

  • A lot of computers have Java, in some form, installed
  • Enables cross-platform enterprise grade application programming
  • Practically everybody heard about it or knows it

This also simplifies my list of assumptions about your machine when following this post:

  • Java (JRE) is installed, mine is 1.7.0_03 for this post), which you probably already have;
  • PHP 5.* (I used 5.3.13 for this one);
  • Windows 7 Home Premium, but this one is not really a requirement.
  • Telnet-client is enabled (if not, press Windows Key+R, type "appwiz.cpl", hit enter and install it via "Turn Windows features on or off" on the left.)

So let's start. Head over to http://james.apache.org/download.cgi#Apache_James_2.3.2_is_the_stable_version and download the Apache James Server. Unpack the download archive to some folder. For this post I used "C:\DevTools\ApacheJames" and I will continue to use this path further on, so keep in mind you should replace it with your own path when running in to it.

Via Windows Explorer go to "C:\DevTools\ApacheJames\bin" and double-click the "run.bat"-file. The Apache James Server is now started and the output should be like:

Using PHOENIX_HOME:   C:\DevTools\ApacheJames
Using PHOENIX_TMPDIR: C:\DevTools\ApacheJames\temp
Using JAVA_HOME:      C:\Program Files\Java\jdk1.7.0_03

Phoenix 4.2

James Mail Server 2.3.2
Remote Manager Service started plain:4555
POP3 Service started plain:110
SMTP Service started plain:25
NNTP Service started plain:119
FetchMail Disabled

The first thing we should do now is add a user to our server from which we can send e-mail and whom can receive e-mail.

Adding users is done by telnetting into the Remote Manager of the Apache James Server. Like the output above shows us, the port we connect to is 4555:

telnet localhost 4555

It now asks for a login and password. This might be strange at first, since you haven't created one yet right? No problem, the Apache James Server's default is using 'root' as both username and password. Enter this and you're in. Ready to do add some users.

To let us add users, they created the 'adduser'-command, which takes a username as first argument and the password used for authentication as second. Again for simplicity just add a user 'test' with the password 'test':

adduser test test

Note that I didn't use a full e-mailaddress, because it results in sort of buggy behavior in the Remote Manager like showing it as user when issuing a 'listusers'-command but not able to delete or verify it using either 'deluser' or 'verify' responding with e.g. "User test@localhost does not exist".

PHP

The only thing to do as far as PHP goes, is verify (and possibly set) two INI-directives:

SMTP = localhost
smtp_port = 25

Test, test, test

Get yourself an e-mailclient, like Windows Live Mail and add an account with the e-mailaddress "test@localhost" with username "test" and password "test".

Now from PHP execute the "mail"-function to test your configuration and test your e-mails on your local machine:

<?php mail("test@localhost", "test subj", "test body", "From: test@localhost"); ?>

conclusion

That was it, I hope I didn't go through it to fast, but there isn't anything much to it. The nice thing about the Apache James Server is you can go as far as testing e-mails as you want, it has a major amount of features and configuring it is very simple.

  • Windows
  • Apache James
  • PHP

Installing the RMySQL package on Windows 7

Posted on by in statistics

introduction

So you want to get statistical? Nowadays one of the ways to go is to use R, mostly in combination with ggplot2 for generating the plots. These plots and graphs however need some data, for that we use data sources. There are a lot of data sources available for us to use and every company and consumer has its own opinion about which one to use, which can even differ per type of usage, application or website. Therefore a lot of database adapters exist for R; one of these adapters is RMySQL, packaged as the RMySQL package.

This post describes the installation of the RMySQL package from beginning to end, assuming you have a 32-bit Windows 7 machine ready.

MySQL

First off we need a working MySQL installation. Head over to http://www.mysql.com/ and download the server of your wishes. Or use, like me, mysql-5.5.17-win32.msi. Follow it's installation instructions and make sure the checkbox next to "Client tools" is checked. I installed it to "C:\Program Files\MySQL\MySQL Server 5.5\", and when you don't, remember to change each of MySQL's paths following. After the installation press the "Windows" key and enter "System", this will open your system's settings screen. Click on "Advanced settings" and choose Environment Variables. When the screen pops up, add a new environment variable for all users and name it "MYSQL_HOME" with a value of "C:\Program Files\MySQL\MySQL Server 5.5\.

This is not all yet! Create the directory "opt" within "C:\Program Files\MySQL\MySQL Server 5.5\lib" and copy-paste "C:\Program Files\MySQL\MySQL Server 5.5\lib\libmysql.lib" to "C:\Program Files\MySQL\MySQL Server 5.5\lib\opt\" and "C:\Program Files\MySQL\MySQL Server 5.5\lib\libmysql.dll" to "C:\Program Files\R\R-2.13.2\bin\i386\".

R

After the installation of MySQL it's time to install R from http://cran.r-project.org/bin/windows/base/, the version I use is 2.13.2. Again follow the installation instructions, choosing for a "Full installation" and a customized startup at the "Startup options" screen to setup your preferred way of display, for me that was: "MDI", "HTML help" and "Internet2" while having "C:\Program Files\R\R-2.13.2" chosen as installation directory.

Now, that was easy, we already installed MySQL and R, what's next?

RTools

For the more difficult part of this post we go to http://www.murdoch-sutherland.com/Rtools/ and install the Rtools for R 2.13.x, i.e. Rtools213.exe, this allows us to build packages for R under Windows. Note that there are other packages and tools available for doing this but in my experience this was the most easy one, so we stick with this one for now.

Install Rtools to "c:\Rtools" and go for a "Custom installation" by checking "Extras to build 32 bit R: TCL/TK, bitmap code, internationalization" and at the "Select R Source Home Directory", paste "C:\Program Files\R\R-2.13.2\src" in the textfield. Click on "Next >" and choose to "Edit the system PATH" by ticking the checkbox to the left of it. The rest speaks for itself.

Rcmd.bat

Download this from http://code.google.com/p/batchfiles/source/browse/trunk/Rcmd.bat into "C:\Program Files\R\R-2.13.2\bin".

RStudio

One application very useful to have, though not necessary is RStudio, downloaded from http://rstudio.org/. With this application we have direct access to the R command line tools, a plots window and more. I recommend downloading this application not only for the purpose of this tutorial but also for upcoming daily(?) usage.

Make sure you download "RStudio 0.94.110 - Windows XP/Vista/7" and choose "C:\Program Files\RStudio" as its installation directory.

RMySQL

Now hit the Windows key and enter "RStudio", and when the program "RStudio" comes up, click it. At the console window type

install.packages('RMySQL',type='source')

and hit Enter. Choose the country most close to you as CRAN mirror for the fastest download, mine is 47.

Note: When the message:

"Warning in install.packages :
  'lib = "C:/Program Files/R/R-2.13.2/library"' is not writable"

shows up, head over to the directory and grant modification rights to the current user (or the RStudio user when installed under its own user profile) and execute the

install.packages('RMySQL',type='source')

command again.

  • R
  • RMySQL
  • Windows

Getting certified, some pro's and misconceptions

Posted on by in resume

Every now and then as developer you're looking at your resume and questioning the number of certificates on it. Whether there is only one, two, or even when there are tens of certificates listed... You still urge yourself to learn, learn and eventually maybe even get certified (again) as reward.

This is exactly the reason why it doesn't matter how many certificates are listed, ever, since you still want to keep on learning and not giving anyone the impression you're standing still doing nothing.

In the community of PHP developers there are more non-certified professionals than certified professionals. PHP developers mostly don't really see the pro's of getting certified. These developers mostly honor the fact that PHP is a quick and easy language to learn and to get started with, and professionalism is found even by building a simple dynamic web page, leaving the discussion of whether this is a good or bad thing for what it is.

Certification can enable employers having more confidence in you as developer, they notice you're doing the best you can to let them know you keep on learning, improving... A glimpse look on your certification-rich resume by HR-departments can even get you on top of the candidates list faster when job-hunting. But there are more benefits to certification, next to the fact your resume looks hotter.

As a developer you can greatly improve your skills when preparing for certification. For example, learning for the PHP 5.3 certification exam practically requires you to have knowledge on multiple topics you might not ever touched before, such as streams, phar and gc. Not because questions on those subjects will be even asked for sure on the exam, but simply the fact that there might be questions on those subjects. Most developers get triggered to learn or rethink something through the fact that they might not know something or are not sure of something.

My own opinion is that certification is nice, and may actually, in this society, really help you getting a job faster, get more respect, and even enhance the speed your learning at. But, yes, there's a but... Take a look at the number of front-end developers out there, doing amazing stuff with HTML5, JavaScript, CSS, just name it... and are they certified? Hell no. Try to name 3 great certification centers for front-end developers, quite hard ain't it?

Front-end developers are probably the most underestimated (web)developers out there, they wipe your ass when you make some back-end component screw up the layout of the site. They provide the user with some occasional spiffy search pages, a window popping out of nowhere improving the user experience, highlighting input boxes when focused. Even providing the users with a gun enabling them to shoot some ducks on the site is no exception.

I chose certification, because it's there and when something's there and can help you improve faster, why not. But don't choose certification just because you will look better, it might to some, but there's a brick wall you'll run into some day.

  • certification
  • resume
  • PHP

Using Enchant with PHP on Windows - Part 1

Posted on by in programming

introduction

PHP 5.3 introduced us to some new bundled extensions, including Enchant. The Enchant extension is a binding for the Enchant library.

Note: Before PHP 5.3, Enchant was available as optional extension through PECL. This article however only covers the setup and usage of the bundled extension in PHP 5.3

This short tutorial gives an introduction to setting up and using this new extension for your PHP projects.

What is Enchant?

Enchant is a library that provides a generic interface to third-party spell-checking APIs (Application Programming Interfaces) and ABIs (Application Binary Interfaces). And this is mainly where the power of Enchant lies in.

Normally if you want to spell-check English and French texts, you'd use the same spell-checking API (e.g. Aspell), because implementing multiple spell-checking libraries would require that you, the developer, knows how to use those specific APIs, each API bringing different methods of checking and correction. But that also means if Aspell, while very good in spell-checking English texts, isn't very strong in spell-checking French texts, while for example Ispell is strong in spell-checking French texts, you cannot bring the same spell-checking quality to the user as he should be able to.

When you use Enchant, the same programming interface can be used for different spell-checking libraries. We are now able to use Aspell for English texts and Ispell for French texts without adding specific Ispell API-calls for checking French texts.

Follow along to see how easy this can be, even on Windows!

Installing Enchant

Installing Enchant is as easy as enabling the extension in your active php.ini. Go to the line that says:

;extension=php_enchant.dll

And change it to:

extension=php_enchant.dll

Now restart your webserver to see these changes live. But that's not all, some more stuff comes to it. Read the next paragraph to find out more!

Using Enchant

As with every extension, PHP provides documentation. For Enchant the documentation can be found on http://www.php.net/enchant

Note: Not every function of the Enchant extension is currently documented. A look inside the Enchant PECL source shows some functions and constants like enchant_broker_get_dict_path which are still on the php.net documentation TODO-list all as far as I noticed.

Nonetheless, a lot of documentation about Enchant can be found at other resources. Since the first day I started programming, I gathered information from as much resources as possible, think of "How to do this in Java" while actually needing to do something in Ruby, or "How to install this in Perl" when needing documentation on installing Ada libraries. This is something that helped me in this situation too; try searching Google on Enchant, PHP and Windows or Ispell and MySpell for that matter and see how many awesome results you get /sarcasm. But lets talk about that in a minute in the next paragraph.

First I want to show you the basic usage of Enchant and how it doesn't work by default in Windows (don't know about Linux/*BSD?) Below is the example taken from php.net:

<?php
$tag = 'en_US';
$r = enchant_broker_init();
$bprovides = enchant_broker_describe($r);
echo "Current broker provides the following backend(s):\n";
print_r($bprovides);

$dicts = enchant_broker_list_dicts($r);
print_r($dicts);
if (enchant_broker_dict_exists($r,$tag)) {
	$d = enchant_broker_request_dict($r, $tag);
	$dprovides = enchant_dict_describe($d);
	echo "dictionary $tag provides:\n";
	$wordcorrect = enchant_dict_check($d, "soong");
	print_r($dprovides);
	if (!$wordcorrect) {
		$suggs = enchant_dict_suggest($d, "soong");
		echo "Suggestions for 'soong':";
		print_r($suggs);
	}
	enchant_broker_free_dict($d);
} else {
}
enchant_broker_free($r);
?>

By default PHP will not go beyond the line saying

if (enchant_broker_dict_exists($r,$tag)) {

We will have to install some dictionaries first.

Installing dictionaries

As I already mentioned above, before you can actually use Enchant's spelling suggestions and other material you have to install some dictionaries. Funny enough, no Windows documentation exists for this yet and even the Linux/*BSD part isn't documented at php.net.

Fortunatly, there are some other resources we can gain information from. First on, I'll show you what I tried before.

In the last paragraph I said not all Enchant's functions were documented at php.net yet, so I looked at the source of the extension, and found a function I thought it'll help me, named enchant_broker_get_dict_path. The function signature for that function is:

string enchant_broker_get_dict_path(resource broker, int dict_type)

This function requires 2 parameters, first the broker resource (created using enchant_broker_init()) and the dict_type, a non-documented constant value. There are 2 possible values for dict_type as indicated by the source, the first one is ENCHANT_ISPELL and the second ENCHANT_MYSPELL, the 2 default included spell-checking providers.

Calling this function however triggered a 500 error on my Apache and crashed my CLI instance of PHP (bug?).

This function already triggerd my brain, as how does it actually know where my dictionaries are installed in the first place? A lot of searches pointed me to this post on Google Groups talking about pyenchant (remember my note on this before?)

So there is also a function allowing me to set this path! Browsing the source again, I stumbled upon enchant_broker_set_dict_path:

bool enchant_broker_set_dict_path(resource broker, int dict_type, string value)

Calling this function above enchant_broker_get_dict_path, fixed the crashing/500 error I got before and actually allowed me to set my dictionary paths:

enchant_broker_set_dict_path($broker, ENCHANT_MYSPELL, 'C:\PHP\enchant\MySpell');
enchant_broker_set_dict_path($broker, ENCHANT_ISPELL, 'C:\PHP\enchant\Ispell');
Note: these directories do not exist by default, so you have to create them, but you can create them anywhere since it doesn't matter where you actually store these dictionaries as long as PHP can access them.

Now we can actually install the dictionaries. Dictionaries can be found at many places, I used the Slovak dictionary found on the Mozilla Wiki's L10n:Dictionaries page.. Extract the downloaded zip-file to your dictionary path (so you get something like C:\PHP\enchant\MySpell\sk_SK.aff and C:\PHP\enchant\MySpell\sk_SK.dic) and refresh your page. As you can see, the dictionary you extracted is now ready to use.

A working example!

Since I already mentioned in the previous paragraph I installed the Slovak (sk_SK) dictionary for the MySpell provider in C:\PHP\enchant\MySpell\, I will just show you the code I use to test all of this; the code I used to get a spelling suggestion for the word "soong" (the word the PHP manual also uses in its example):

<?php
$broker = enchant_broker_init();
$tag = 'sk_SK';

enchant_broker_set_dict_path($broker, ENCHANT_MYSPELL, 'C:\PHP\enchant\MySpell');

if (enchant_broker_dict_exists($broker, $tag)) {
	$dict = enchant_broker_request_dict($broker, $tag);
	$word = 'soong';
	$isCorrectlySpelled = enchant_dict_check($dict, $word);

	if ($isCorrectlySpelled !== true) {
		$suggestions = enchant_dict_suggest($dict, $word);

		echo nl2br(print_r($suggestions, true));
	} else {
		echo 'The word is correctly spelled!';
	}
}

enchant_broker_free($broker);
?>
also posted on pastebin http://pastebin.com/gdKqVX2C

If you followed all my instructions above the following should be on your screen right now:

Array
(
	[0] => song
)

conclusion

It took me a lot of time figuring out how this all worked and I hope I helped anyone reading this article looking for some information on how to set all this up. In the next part I'll go up and do some more funky stuff using Enchant. Stay tuned!

  • PHP
  • Enchant
  • Windows