Saturday, November 6, 2010

Using DBI putting field / column names in a hash table

use DBI;
use DBD::mysql;
use strict;
use warnings;

my $database = "ci";
my $host = "localhost";
my $port = "3306";
my $tablename = "happy";
my $user = "root";
my $pw = "wouldntyouliketoknow";

my $dsn = "dbi:mysql:$database:localhost:3306";
my $connect = DBI->connect($dsn, $user, $pw);

my $query = "SHOW COLUMNS FROM $tablename";
my $sth = $connect->prepare($query);
my %hash;
my @ary;
while(@ary = $sth->fetchrow_array){
$hash{$ary[0]}= '';
my $key;
my $value;

print "\n";
while (($key, $value) = each(%hash)){
print $key.", ".$value."\n";

Same as the perl script before, but using DBI module instead of Mysql Module.

No comments: