Stellarcore.net

Stellarcore.net

This script is really just an example for a flexible query. It loads the results in a Hash which can then be used in many differnt ways.

#!/usr/bin/perl

use strict;
use DBI;

##################
#INIT set table via input
my $table = shift @ARGV;

##################
#SQL Setups
my $dbh = DBI->connect("DBI:mysql:scn_accounts:localhost", "user", "password");
my $sql = "select * from $table";

#################
#SQL Query
my $sth = $dbh->prepare( $sql );
my $rc = $sth->execute();

my @fields = @{ $sth->{NAME} };

my %results;

while (my @ary = $sth->fetchrow_array()) {
	my $hash_key = shift @ary;
	$results{$hash_key} = [@ary];
	}

$sth->finish(); # we're done with this query

my @KEYS = (sort { $results{$a} <=> $results{$b} } keys %results);

###############
#RESULTS
my $counter = 0;

for my $key (@KEYS) {
	print "\n$fields[$counter] \t $key \n";
	$counter++;
	for my $data ( @{$results{$key}} ) {
		print "$fields[$counter] \t $data \n";
		$counter++;
		}
	$counter = 0;
	}

$dbh->disconnect();

exit;