All posts by WhiteWind

About WhiteWind

I'm a Perl-PHP Developer from India. I'm a great fan of Wordpress & Perl.

Perl DBI AutoCommit handling for transaction safe Database

AutoCommit Option

If your transactions are simple, you can save yourself the trouble of having to issue a lot of commits. When you make the connect call, you can specify an AutoCommit option which will perform an automatic commit operation after every successful query. Here’s what it looks like:

my $dbh = DBI->connect($dsn, $userid, $password,{AutoCommit => 1}) 
              or die $DBI::errstr;

Here AutoCommit can take value 1 or 0, where 1 means AutoCommit is on and 0 means AutoCommit is off.
In normal we use “AutoCommit in on mode“. But in the case of Insertion and updation for a transaction safe DB we have to make “AutoCommit in off mode” temporarily. Currently most of us try to write separate function as follows:

my $dbh1 = DBI->connect($dsn, $userid, $password,{AutoCommit => 0}) 
              or die $DBI::errstr;

 Actually we don’t need to write such a thing. we can handle it with

$dbh->begin_work

Begin Transaction

Many databases support transactions. This means that you can make a whole bunch of queries which would modify the databases, but none of the changes are actually made. Then at the end you issue the special SQL query COMMIT, and all the changes are made simultaneously. Alternatively, you can issue the query ROLLBACK, in which case all the changes are thrown away and database remains unchanged.

Perl DBI module provided begin_work API, which enables transactions (by turning AutoCommit off) until the next call to commit or rollback. After the next commit or rollback, AutoCommit will automatically be turned on again.

$rc  = $dbh->begin_work  or die $dbh->errstr;

COMMIT Operation

Commit is the operation which gives a green signal to database to finalize the changes and after this operation no change can be reverted to its orignal position.

Here is a simple example to call commit API.

$dbh->commit or die $dbh->errstr;

ROLLBACK Operation

If you are not satisfied with all the changes or you encounter an error in between of any operation , you can revert those changes to use rollback API.

Here is a simple example to call rollback API.

$dbh->rollback or die $dbh->errstr;

How to get selected text from drop down list (select box) using jQuery

It can be done as follows :

$("#dropdownid option:selected").text();

Perl Script to calculate and show age based on medical standards.

It’s as follows :-

sub age {
    use Time::Local;
    use Date::Calc qw(Delta_Days);
    # Assuming $birth_month is 0..11
    my ($birth_day, $birth_month, $birth_year) = @_;
    my ($day, $month, $year) = (localtime)[3,4,5];
    $year += 1900;
    $month+=1;
    my @today_sec = localtime();
    my $time = timelocal(@today_sec);
    my @birthday_sec = (0, 0, 0, $birth_day, $birth_month, $birth_year);
    my $birthtime = timelocal(@birthday_sec);
    my @birthday=($birth_year, $birth_month, $birth_day);
    my @today=($year, $month, $day);
    my $days = Delta_Days(@birthday, @today);
    my $netage="";
    if($days > 28) {
        my $age = $year - $birth_year;
        $age-- unless sprintf("%02d%02d", $month, $day)
          >= sprintf("%02d%02d", $birth_month, $birth_day);
        my $mnth=($month>$birth_month)?$month-$birth_month:12+($month-$birth_month);
        my $mnth_total=($age*12)+$mnth;
        if($mnth_total > 0 && $mnth_total < 4) {
            my $week=int($days/7);
            if($week > 1) {
                $netage = $week." Weeks";
            } else {
                $netage = $week." Week";
            }
        } elsif($mnth_total >= 4 && $mnth_total < 24) {
            $netage = $mnth_total." Months";
        } elsif($age >= 2 && $age < 18) {
            $netage = $age." Years";
            if($mnth == 1) {
                $netage .= " ".$mnth." Month";
            } elsif($mnth > 1) {
                $netage .= " ".$mnth." Months";
            } else { }
        } elsif($age >= 18) {
            $netage = $age." Years";
        } else { }
    } else {
        $netage = $days." Days";
    }
    return $netage;
}

print &age(6,4,1990);

Output :-

24 Years

Perl Program to calculate age

To calculate the age, we can use following subroutine :

print &age($tmpdob[2],$tmpdob[1],$tmpdob[0]);
sub age {
           # Assuming $birth_month is 0..11
           my ($birth_day, $birth_month, $birth_year) = @_;
           my ($day, $month, $year) = (localtime)[3..5];
          $year += 1900;
          $month+=1;
          my $age = $year - $birth_year;
          $age-- unless sprintf("%02d%02d", $month, $day)
            >= sprintf("%02d%02d", $birth_month, $birth_day);
          my $mnth=($month>$birth_month)?$month-$birth_month:12+($month-$birth_month);
          return $age."Y/".$mnth."M ";

}

How to create a folder in PHP?

In PHP we can create a folder using mkdir() function.

eg:

>?php
       mkdir("testing");
?>