To add to the other comments:

A database handle is an object the represents your connection to a database. It is represented by the scalar you define it to. So as was pointed out earlier, you can create as many as you would like.

my $dbname = 'production'; my $username = 'code_monkey'; my $password = '!#@&*'; # You can also specify the database here instead of using the '$dbh->d +o("use $dbname");' my $dbh_foo = DBI->connect("dbi:Sybase:server=Foo;database=$dbname",$u +sername,$password ); # Here you can call a second one my $dbh_bar = DBI->connect("dbi:Sybase:server=Bar;database=$dbname",$u +sername,$password ); # You can even call a completely different RDBMS my $dbh_baz = DBI->connect("dbi:Pg:host=baz;dbname=$dbname",$username, +$password );

The same goes for $sth Statement Handles (which belong to their respective Database Handle). So you can create several statement handles or reuse the same one (generally with Placeholders and Bind Values). Since you don't appear to be using Placeholders and Bind Values, I will say that they are highly encouraged, as they handle quoting correctly, offer a performance increase for multiple calls, and prevent possible SQL Injection attacks.

my $pet = '5" Stuffed Camel'; my $sql = 'SELECT id,name FROM users WHERE has_pet = ?'; # '?' signifies a placeholder my $sth_sel = $dbh->prepare($sql); $sth_sel->execute($pet); # Now $pet is properly quoted
Or reuse Statement Handles for some performance gain:
my $sql = 'SELECT id,name FROM users WHERE has_pet = ?'; my $sth_sel = $dbh->prepare($sql); foreach my $pet (qw/ monkey bear tiger /) { $sth_sel->execute($pet); }

I can't stress enough that you should be using Placeholders and Bind Values for slmost everything DBI related.

grep
XP matters not. Look at me. Judge me by my XP, do you?


In reply to Re: database connection output by grep
in thread database connection output by dreman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.