> Im trying to list a record once and then count how many records. But I
keep getting a list of all the records and it counts those?? I read in a
file with a list of rooms and then look for each one that matches dept
22 and Im trying to show it once and then count how many for the building?
> 
> 
> #!/usr/bin/perl
> use strict;
> use DBI;
> use warnings;
>  
> my $list = "/etc/cron.d/room.txt";
> my $dbh = DBI->connect("dbi:mysql:dept","user","")
>         or die("Couldn't connect");
> my( $date, $build, $room, $dept );

Why do you declare the above variables at this scope level, and again
inside the while loop?

> my $cnt = 1;

You have initialized your counter here, which it should be reset each
time through the loop, that should fix your immediate problem. But
should you be setting this to zero, what if there are no results?

> open (LIST, "$list") or die "read error";
>  
> while (<LIST>) {
> chomp;
> print "For $_ :\n";
> my $sql = "select date,build,room,dept from Jun04 where room = '$_'
and dept = '22' order by date";

You should consider using placeholders for the above so that you don't
have to handle the quoting yourself, especially since you have already
broken the DBI steps into prepare/execute.

> my $sth = $dbh->prepare( $sql );
> $sth->execute();
> my( $date, $build, $room, $dept  );
> $sth->bind_columns( undef, \$date, \$build, \$room, \$dept );
>  

What is this 'undef' up to?

>  
> while( $sth->fetch() ) {
> print "For $room :\n";
> print $cnt++;print "BLD:$build\n";
> }
> $sth->finish();
>  }
>  
> $dbh->disconnect();
> 
> 
> goal output:
> For 30 :
> 10BLD:211
> For 20 :
> 2BLD:1544
> 
> 
> Current output:
> For 30 :
> 1BLD:211
> 2BLD:211
> 3:BLD211
> .
> .
> 10BLD:211
> For 20 :
> 11BLD:1544
> 12BLD:1544
> 

Not sure what your overall end goal is, or what DB engine you are using,
but if counting is your only goal and you have no other reason to select
specific fields you should consider letting the DB handle it.  Most
support some sort of counting ability.

http://danconia.org


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to