CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



kate212
New User

Aug 28, 2013, 6:13 PM

Post #1 of 3 (784 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;


BillKSmith
Veteran

Aug 30, 2013, 4:35 AM

Post #2 of 3 (762 views)
Re: [kate212] Select return different value [In reply to] Can't Post

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


FishMonger
Veteran / Moderator

Aug 30, 2013, 6:48 AM

Post #3 of 3 (761 views)
Re: [kate212] Select return different value [In reply to] Can't Post

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.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives