Some recent discussion in the chatterbox showed a few people hadn't seen bind_columns, so I grabbed some code I recently wrote using that, abstracted it a bit, and included it here as a cut-n-paste model of how to use that. Enjoy!
use DBI; my $dbh = DBI->connect("dbi:mysql:database", "user", "password", { RaiseError => 1 }); $dbh->do("SET OPTION SQL_BIG_TABLES = 1"); # for mysql my $sth = $dbh->prepare(<<'_END'); SELECT field1, field2, sum(field3) as Total FROM some_database WHERE field2 LIKE 'random%' AND field1 BETWEEN ? and ? GROUP BY field1, field2 ORDER BY Total DESC _END $sth->execute('a', 'k'); $sth->bind_columns(\my($field1, $field2, $sum)); while ($sth->fetch) { printf "%20s %20s %s\n", $field1, $field2, $sum; } $sth->finish; $dbh->disconnect;

In reply to template for using bind_columns with DBI by merlyn

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.