Thursday, January 13, 2011

Selecting random record in MySQL

The random number generators are often used in almost all  kinds of application. The random numbers are generated using well known random number generator API. But what about selecting a random record from a database. There are cases we need this. for example- if you want to suggest a random picture to user in a web application. Pictures are stored in database. Fortunately databases provide this kind of feature but unfortunately different databases have different query to achieve this. Here is how this is achieved in MySQL  - 
SELECT columnName FROM tableName

The value of limit can be changed to number of records to fetch.

Tuesday, January 11, 2011

Scripting langauges

I have always been working with the compiled and statically typed languages like C, C++, Java, C#. I used to avoid scripting languages and also used to hate little bit these scripting languages. But recently started exploring PHP and JavaScript and it changed my perception. Now I started loving these languages. Programming in the these languages needs little bit of different mind set. It takes some time to think in terms of loosely typed coding style.

There are, of course, pro and cons. Here is what I have started observing about the scripting languages -

- You dont need to think in terms of types of data. Most of the time this is very good but some time this is very problematic, especially when you are dealing with string and integer together. In such case you need to explicitly cast to other type. Here is one PHP code which confused me at first glance -
$var = "false";
if ($var) {
echo "This is true";
} else {
echo "This is false";

First time when I was dealing with similar code, I was expecting it to print "false". But this prints true. I was thinking, as this is loosely typed language, it will convert the string to boolean automatically. But later found that the string is converted to boolean on the basis of null check. There are other instances, which are potentially confusing. This proves that the these languages are not completely type-less. Sometime you are forced to think in terms of their type and and some complicated rules about the conversion from one type to another.

- You dont need to compile! good or bad?? As I am coming form the background of complied languages, I am feeling this is as a bad thing. The compiler catches lot of error at the compile time but here I have to wait for runtime even for common syntax errors.

- Scripting languages saves you from writing lot of code that you end up writing in languages like Java or C#. Particularly writing the interfaces and thinking what class will implement what interface. As the function name and variable name resolution happens at run time, you can pass around object of any class provided that object
has that particular method or variable with that.

- The scripting language makes your life easier for most frequent operation on string, array, pattern matching etc.