in reply to Best practices for closing database connections?
But, since you are trying to fix an old script, you might not be interested in totally rewriting it with those new tools. So assuming you are running a process per request like classic CGI, you just need to create some kind of global database handle and then refer back to it any time you need to run a query. There are modules that can do this for you, but you *could* do something as simple as this:
// in the main CGI script sub dbh { $main::dbh ||= DBI->connect(...); }
where you just refer to main::dbh() every time you want to access your connection.main::dbh()->selectall_arrayref(...)
Solutions using purpose-build modules will be prettier than this, but also have a little more learning curve.
As a side-note, I 100% agree with everyone else that you should unconditionally use query placeholders for every query.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Best practices for closing database connections?
by Jenda (Abbot) on Mar 22, 2022 at 15:25 UTC | |
by marto (Cardinal) on Mar 22, 2022 at 15:47 UTC |