
kate212
New User
Aug 28, 2013, 6:13 PM
Post #1 of 3
(2690 views)
|
Select return different value
|
Can't Post
|
|
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: #!/usr/bin/perl use strict; use warnings; use DBI; my $driver = "mysql"; my $database = "database"; my $user = "user"; my $password = "password"; my $dbh = DBI->connect( "DBI:$driver:$database", $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 # $dbh->disconnect; exit 0;
|