J
Jon
Hiya,
I'm trying to tidy up my code a bit by creating an 'SQL' package. At the
moment to interface with my db I'm doing something like..
sub checkpass
{
my ($username, $password) = @_;
my $dbh = DBI->connect("DBI:mysql:database", "username", "password")
unless ($dbh) { die($DBI::errstr); }
my $sql = "SELECT id FROM users WHERE username=? AND password=?";
my $sth = $dbh->prepare($sql);
$sth->execute( $username, $password) || die($sth->errstr());
my $rows = $sth->rows;
$sth->finish();
$dbh->disconnect();
return $rows;
}
I'd like to be able to create a package so I only need to connect the
database once and I can disconnect when the script has finished. eg:
my $dbh = SQL->new();
if ($dbh->checkpass()) { #authed } else { #notauthed }
I've tried the code below but get it dies with "Can't locate object method
"prepare" via package "SQL" at /SQL.pm line 19. I'm pretty new to OO so I
can't figure out what I'm doing wrong. I understand that its trying to call
on SQL:
repare but I don't know how I can make it not do that. I have a
feeling its got something to do with me re-blessing $dbh which has already
been blessed but would like someone with more experience to confirm that if
possible. If I'm wrong could someone please point me in the right
direction? Thanks for your time.
=test.pl=
#!/usr/bin/perl
use lib './';
use SQL;
use strict;
my $dbh = SQL->new();
if ($dbh->checkpass( "username", "password" ))
{
print "authed";
}
else
{
print "not authed";
}
=SQL.pm=
Package SQL;
use DBI;
use strict;
sub new
{
my $dbh = DBI->connect("DBI:mysql:database", "username", "password")
unless ($dbh) { die($DBI::errstr); }
bless $dbh, 'SQL';
return $dbh;
}
sub checkpass
{
my ($dbh, $username, $password);
my $sql = "SELECT id FROM users WHERE username=? AND PASSWORD=?";
my $sth = $dbh->prepare($sql);
$sth->execute( $username, $password ) || die($sth->errstr));
my $rows = $sth->rows;
return $rows;
}
1;
I'm trying to tidy up my code a bit by creating an 'SQL' package. At the
moment to interface with my db I'm doing something like..
sub checkpass
{
my ($username, $password) = @_;
my $dbh = DBI->connect("DBI:mysql:database", "username", "password")
unless ($dbh) { die($DBI::errstr); }
my $sql = "SELECT id FROM users WHERE username=? AND password=?";
my $sth = $dbh->prepare($sql);
$sth->execute( $username, $password) || die($sth->errstr());
my $rows = $sth->rows;
$sth->finish();
$dbh->disconnect();
return $rows;
}
I'd like to be able to create a package so I only need to connect the
database once and I can disconnect when the script has finished. eg:
my $dbh = SQL->new();
if ($dbh->checkpass()) { #authed } else { #notauthed }
I've tried the code below but get it dies with "Can't locate object method
"prepare" via package "SQL" at /SQL.pm line 19. I'm pretty new to OO so I
can't figure out what I'm doing wrong. I understand that its trying to call
on SQL:
feeling its got something to do with me re-blessing $dbh which has already
been blessed but would like someone with more experience to confirm that if
possible. If I'm wrong could someone please point me in the right
direction? Thanks for your time.
=test.pl=
#!/usr/bin/perl
use lib './';
use SQL;
use strict;
my $dbh = SQL->new();
if ($dbh->checkpass( "username", "password" ))
{
print "authed";
}
else
{
print "not authed";
}
=SQL.pm=
Package SQL;
use DBI;
use strict;
sub new
{
my $dbh = DBI->connect("DBI:mysql:database", "username", "password")
unless ($dbh) { die($DBI::errstr); }
bless $dbh, 'SQL';
return $dbh;
}
sub checkpass
{
my ($dbh, $username, $password);
my $sql = "SELECT id FROM users WHERE username=? AND PASSWORD=?";
my $sth = $dbh->prepare($sql);
$sth->execute( $username, $password ) || die($sth->errstr));
my $rows = $sth->rows;
return $rows;
}
1;