Home: Perl Programming Help: Beginner:
Select return different value

New User

Aug 28, 2013, 6:13 PM

Views: 2732
Select return different value

Hi There,

I am a beginner in perl, working in a simple project. I did some code to connect a database and export the data to a CSV file, however, it was working fine but now when exporting the data the first column (for example "Person_ID") list in a sequence number (1 2 3 ...) instead of the original values (10 12 21 ...) then the rest of the columns are fine.

This is my code:


use strict;
use warnings;
use DBI;

my $driver = "mysql";
my $database = "database";
my $user = "user";
my $password = "password";

my $dbh = DBI->connect(
$user, $password,
RaiseError => 1,
PrintError => 1,
) or die $DBI::errstr;

#Simple select for the table
my $sth = $dbh->prepare("SELECT * FROM persona") or die $dbh->errstr;

open my $fh, '>', 'Output.csv' or die "Could not open file Output.csv: $!";
print $fh qq{Person_ID,Person_name\n};

$sth->execute() or die $dbh->errstr;

# Output the results
print "Person_ID \t Person_name\t\n";
while (my $results = $sth->fetchrow_hashref) {
# print $results->{Person_ID}." \t". $results->{Person_name}."\t";

#print "\n";
print $fh qq{$results->{'Person_ID'},$results->{'Person_name'},\n};

close $fh;

# Disconnect from database

exit 0;


Aug 30, 2013, 4:35 AM

Views: 2710
Re: [kate212] Select return different value

I do not see any error. Are you sure that your database is correct?
Good Luck,

Veteran / Moderator

Aug 30, 2013, 6:48 AM

Views: 2709
Re: [kate212] Select return different value

Sounds like your 'Person_ID' column doesn't hold the exact values that you expect and if that's the case, you'll need to look into how the records were inserted and the definition of the persona table structure.