in reply to DBI / Mysql connections
my $dbh = dbConnect(); sub dbConnect { my $dsn = "DBI:mysql:database:localhost"; my $db_user_name = 'username'; my $db_password = 'password'; $dbh = DBI->connect ($dsn, $db_user_name, $db_password, {RaiseError => 1}) || die("cannot connect to DB: ".DBI::errstr."\n",$dbh); } sub foo { # Can still see $dbh in here my $sth = $dbh->prepare(...); }
Of course, this means you're connecting to the DB every time the script's called (which might not be necessary), and you won't be able to access $dbh from outside of this package (probably a good thing though, I prefer to confine all DB related stuff to one package, or at least one hierarchy of classes. The next step after this is Class::DBI).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: DBI / Mysql connections
by radiantmatrix (Parson) on Dec 03, 2004 at 15:48 UTC |