Gossamer Forum
Home : General : Databases and SQL :

print out group by result

Quote Reply
print out group by result
In command line below code works ok.

mysql> SELECT studentstatus, COUNT(*)FROM studentdb GROUP BY studentstatus;

the result wil be :

status1 20

status2 40

status3 18



My question is how to make these value print out in webpage. any code could be used?

I could not know how to handle COUNT(*) value.



Thanks
Quote Reply
Re: [courierb] print out group by result In reply to
If you have Perl, then you need to install DBI and DBD::mysql. Once you do that, you need to write a Perl program, which does something like:
Code:
#!/usr/local/bin/perl
use DBI;
use CGI::Carp qw(fatalsToBrowser);

my $dbh = DBI->connect("DBI:mysql", "username", "password") or die DBI->errstr;
my $sth = $dbh->prepare("select studentstatus, count(*) from studentdb group by studentstatus") or die DBI->errstr;
$sth->execute() or die DBI->errstr;
print "Content-type: text/plain\n\n";
while (my $rec = $sth->fetchrow_arrayref()) {
print "$rec->[0] : $rec->[1]\n";
}
$dbh->disconnect();

--Philip
Links 2.0 moderator
Quote Reply
Re: [sponge] print out group by result In reply to
Thanks very much sponge . it works nicely now.