Gossamer Forum
Home : General : Perl Programming :

Can you track wats the problem in this code !!!!

Quote Reply
Can you track wats the problem in this code !!!!
I am newbie in cgi programming.I have a problem in this program.
--------------------------------------------------------------------------------------------------------------
#!/usr/bin/perl -wT
use CGI qw/:standard/;
use warnings;
use diagnostics;
use strict;
use DBI();
print "Content-type:text/html\n\n";
my @row;
my $a="PortAransas";
print header,start_html('A Simple Example'),h1('A Simple Example');
my $dbh=DBI->connect("DBI:mysql:database=test",undef,undef,{ RaiseError => 1, AutoCommit => 0 })||die "Unable to connect $DBI::errstr";
my $sth = $dbh->prepare("select * from TAMUCCneuralnet where station=?")|| die "Unable to insert $DBI::errstr";
$sth = $dbh->execute($a);
while(@row=$sth->fetchrow_array())
{
print @row;
}
$sth = $dbh->finish();
$dbh->disconnect();
end_html;
--------------------------------------------------------------------------------------------------------------
Error : Can't locate object method "execute" via package "DBI::db" at ./tp.cgi line 12.

THE TABLE EXISTS AND I'M NOT GETTING ANY RESULTS.CAN SOMEONE HELP ME WITH THIS....
Quote Reply
Re: [karthik] Can you track wats the problem in this code !!!! In reply to
Hi karthik,

Code:
#!/usr/bin/perl -wT
use CGI qw/:standard/;
# use warnings; Don't need this if you have the -w in shebang
use diagnostics;
use strict;
use DBI();

# You're printing the header again below. Only need to print once
# print "Content-type:text/html\n\n";

my $dbh = DBI->connect("DBI:mysql:database=test",undef,undef,{
RaiseError => 1, AutoCommit => 0
}) or die "Unable to connect $DBI::errstr";

my $a="PortAransas";

print header,start_html('A Simple Example'),h1('A Simple Example');

my $sth = $dbh->prepare("select * from TAMUCCneuralnet where station=?")
or die "Unable to insert $DBI::errstr";

#$sth = $dbh->execute($a);
# Should be:
$sth->execute($a);

while(my @row = $sth->fetchrow_array()) {
print @row;
}
$dbh->disconnect();

print end_html;

Not tested but that should help.

~Charlie
Quote Reply
Re: [Chaz] Can you track wats the problem in this code !!!! In reply to
It generates output when executed using ./tp.cgi.. but does'nt show any output in html pages when accessed on web !!!

#!/usr/bin/perl -T
use CGI qw(:standard :html3);
use warnings;
use diagnostics;
use strict;
use DBI();
my @row;
my $a="PortAransas";
print header,start_html('Search Results');
my $dbh=DBI->connect("DBI:mysql:database=test",undef,undef,{RaiseError => 1, AutoCommit => 0 })||die "Unable to connect $DBI::errstr";
my $sth = $dbh->prepare("select * from TAMUCCneuralnet where STATION=?")|| die "Unable to insert $DBI::errstr";
$sth->execute($a);
print h1("Results"), "<TABLE BORDER=1>";
while(@row=$sth->fetchrow_array())
{
print Tr( td( \@row ) );
}
print "</TABLE>\n";
$sth->finish;
$dbh->disconnect;
print end_html();
Quote Reply
Re: [karthik] Can you track wats the problem in this code !!!! In reply to
1) What is the output of ./tp.cgi?
2) Any errors in your error log?

The code you have posted works for me.

~Charlie