Wednesday, February 24, 2010

PERL connect to DBI subroutines (smart detect)

Here are the codes:


sub connect_db {
 if (!$connect) {
  $dbh = DBI->connect("dbi:mysql:your_db_name;host=localhost:3306;user=db_user;password=user_password") or die "Couldn't connect to database: DBI::errstr\n";
  $connect = 1;
 }
}


sub disconnect_db {
 $dbh->disconnect;
 $connect = 0;
}


Try to use as little disconnect as possible as connecting and disconnecting may cause delay and to the HTML connection. As you may already know you may need to install the DBI module for PERL before running these codes. You also need to create a database and obtain the database name, user id and password in order to connect to the database.

No comments:

Post a Comment