On 03/14/2007 05:37 AM, Umar Draz wrote:
HI dear members! I have a file called dbcon.pl into include directory.
#!/usr/local/bin/perl
use DBI;
# Database Username, Password and Driver settings
my $user="konsole";
my $pass="send";
my $host="localhost";
my $driver="mysql";
my $db="treat";
my $tnt="xyz";
# Connect Database
my $dsn = "DBI:$driver:database=$db;host=$host";
my $dbh = DBI->connect($dsn, $user, $pass);
$dsn and $dbh are lexical variables that are restricted to dbcon.pl. You
might want to consider using package variables. e.g:
our $dbh = ...blah blah
Now I have created a another file login.pl there i include dbcon.pl using
require.
--------------
#!/usr/local/bin/perl -w
require "include/dbcon.pl";
use warnings;
Missing:
use strict;
use CGI;
use CGI::Session;
my $cgi = new CGI;
my $login = $cgi->param('username');
my $passwd = $cgi->param('password');
print
$cgi->header().
$cgi->start_html(-title=>'My Login Page');
if($cgi->param("Submit"))
{
my $sth = $dbh->prepare(qq{ select username, password from login where
username=\'$login\' });
If you had enabled strictures ("use strict;"), this line wouldn't have
compiled, and that would've prevented you from making an error.
Since $dbh in dbcon.pl was declared with "my," it was restricted to
dbcon.pl, and so it wasn't available in login.pl, so $dbh in login.pl as
undefined. Undefined values are not objects (well, maybe they are in
Smalltalk ;-) , but Perl isn't Smalltalk).
$sth->execute();
# Get the Result
my ($username, $password) = $sth->fetchrow_array();
$sth->finish();
# checking valid user name
if ($username eq $login and $password eq $passwd)
{
print "Your User name and password accepted"
}
else { print "Wrong username or password"; }
}
$cgi->end_html;
exit(0);
-----------------------
but when i click on submit button its not working then I saw my apache errors
logs there was a line.
Name "main::dbh" used only once: possible typo at /var/www/myinventory/login.pl
line 19.
Can't call method "prepare" on an undefined value at
/var/www/myinventory/login.pl line 19.
Please help me how i can solve this problem
Regards,
Umar Draz
---------------------------------
The fish are biting.
Get more visitors on your site using Yahoo! Search Marketing.
Most Perl programmers would use package variables and the Exporter.
KISS Method:
In file dbcon.pl:
our $dbh = DBI->connect($dsn, $user, $pass);
In file login.pl: (everything stays the same)
Standard Method:
In file dbcon.pl:
package Mydbcon;
use Exporter qw(import);
@EXPORT = qw($dbh);
...
our $dbh = DBI->connect($dsn, $user, $pass);
In file login.pl
require 'include/dbcon.pl';
import Mydbcon;
Exotic Method :-) :
In file dbcon.pl: (everything stays the same)
In file login.pl:
my $dbh = require 'include/dbcon.pl';
The exotic method relies on the fact that 'require' returns the last
scalar value in the script 'dbcon.pl'; that happens to be $dbh in this case.
The KISS (keep it simple stupid) and exotic methods are easiest
probably, but the standard method is more flexible in the long run. Read
"perldoc Exporter" to find out how to use the exporter.
I hope this helps.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/