Connecting to a MYSQL Database using Perl
Example of a select returning more than one row
#!/usr/bin/perl
use DBI;
$|=1;
$dsn = "DBI:mysql:mydatabase;localhost";
$dbh = DBI->connect($dsn, 'username', 'password');
if ( !defined $dbh ) {
exit;
}
# use the four lines below if the query results
# in many rows or if you get 'out of memory' errors
#$SQL_QUERY="SET SQL_BIG_TABLES=1";
# $cursor = $dbh->prepare( "$SQL_QUERY" );
# $cursor->execute;
# $cursor->finish;
$SQL_QUERY=<<__CURSOR_1__;
select col_1, col_2, col_3, col_4
from table_1
where table_1.col_1 = 'X'
order by col_2
__CURSOR_1__
$cursor = $dbh->prepare( "$SQL_QUERY" );
$cursor->execute;
while ( @row = $cursor->fetchrow ) {
print "@row\n";
}
$cursor->finish;
$dbh->disconnect;