Home > Archive > PERL Modules > December 2006 > How to include a connection file in perl
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to include a connection file in perl
|
|
| sulata@gmail.com 2006-12-02, 6:56 pm |
| Hello
I am shifting from php to perl and I need some help. I want to make a
database insert page and divide that page into two parts. One is the
connection file and other is the execution file. When all the following
code is in one file, it works well, but when I break it into two files,
this code does not run. Can anyone please help? Maybe I am not using
'require' properly. My code is;
# load module
use DBI;
# connect
my $dbh = DBI->connect("DBI:mysql:database=perl;host=localhost",
"root", "", {'RaiseError' => 1});
$dept_name = time();
# execute INSERT query
my $rows = $dbh->do("INSERT INTO departments (dept_name) VALUES
('".$dept_name."')") or die "Error";
print "Content-type: text/html\n\n";
print "Done: $rows row(s) affected\n";
# clean up
$dbh->disconnect();
| |
| xhoster@gmail.com 2006-12-03, 9:57 pm |
| sulata@gmail.com wrote:
> Hello
>
> I am shifting from php to perl and I need some help. I want to make a
> database insert page and divide that page into two parts. One is the
> connection file and other is the execution file. When all the following
> code is in one file, it works well, but when I break it into two files,
> this code does not run. Can anyone please help? Maybe I am not using
> 'require' properly. My code is;
>
> # load module
> use DBI;
> # connect
> my $dbh = DBI->connect("DBI:mysql:database=perl;host=localhost",
> "root", "", {'RaiseError' => 1});
> $dept_name = time();
> # execute INSERT query
> my $rows = $dbh->do("INSERT INTO departments (dept_name) VALUES
> ('".$dept_name."')") or die "Error";
> print "Content-type: text/html\n\n";
> print "Done: $rows row(s) affected\n";
> # clean up
> $dbh->disconnect();
foo.pm:
package foo;
sub connect {
DBI->connect("DBI:mysql:database=perl;host=localhost",
"root", "", {'RaiseError' => 1});
}
1;
__END__
file.pl:
use foo;
my $dbh=foo::connect();
# etc.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
|
|
|
|
|