Gossamer Forum
Home : General : Perl Programming :

CGI/Access error: can't call method "sql" on an undefined value

Quote Reply
CGI/Access error: can't call method "sql" on an undefined value
I’m using strict, CGI::Carp qw(fatalsToBrowser), and win32::ODBC. I used “my” to clean up global variable errors identified by strict. But I still get this error: Can’t call method “Sql” on an undefined value.

The code is fairly simple. I’m basically testing to see if everything works properly before going on to a more elaborate script. I've sprinkled "prints" throughout to get at the variable values at a particular time. I've bolded the line that produces the error.

# linking up with a the Northwind Database
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use win32::ODBC;
use strict;
print header(), start_html("Dictionary"), h1("Dictionary");
print hr(); #draw a hr
print start_form();
print p("What's your word? ", textfield("word"));
print p(submit("submit"), reset("clear"));
print end_form(), hr();
my $response;
my $word;
if (param()) {
$word = param("word"); # initially, this will be a two digit number representing ProdID
chomp $word;
print p("the current word is $word");
init_word($word);
print p("the current word is $response");
if ($response ne " ") {
print p("Your definition is $response.");
} else {
print p("You blew it!");
}
}

sub init_word {
my $DSN;
my $db;
my %Data;
my $SqlStatement;
$DSN = "Northwind";
if (!($db = new Win32::ODBC($DSN),"admin"," ")){
print p("Error connecting to $DSN\n");
print p("Error: " . Win32::ODBC::Error() . "\n");
exit;
}

print p("the current word is sub $word");

$SqlStatement = "SELECT * FROM Products";
if ($db->Sql($SqlStatement)){
print p("SQL failed.\n");
print p("Error: " . $db->Error() . "\n");
$db->Close();
exit;
}

print p("the current word is next sub $word");

while($db->FetchRow()){
%Data = $db->DataHash();
if ($Data{ProductID} eq $word) {
$response = $Data{UnitsInStock}
}

}

$db->Close();

}
print end_html