
Untitled
By: a guest on
Jul 24th, 2012 | syntax:
None | size: 1.29 KB | hits: 13 | expires: Never
Perl to PHP MySQL OOP example [closed]
sub new {
my $class = shift;
my $Config = Config::Config->new;
my %params = @_;
my $key = undef; my $value = undef;
foreach my $tmp (keys %params) { $key = $tmp; $value = $params{$key}; }
if (! $key) { return undef; }
my $dbh = DBI->connect($Config->{'db_source'}, $Config->{'db_username'}, $Config->{'db_password'}) or return undef;
my $sth = $dbh->prepare("select * from Accounts where ($key = ?) limit 1") or return undef;
my $res = $sth->execute($value) or return undef;
my %Data;
while (my $hashref = $sth->fetchrow_hashref) { %Data = %{$hashref}; }
$sth->finish; $dbh->disconnect;
if (! %Data) { return undef; }
bless { %Data }, $class;
}
sub Delete {
my $self = shift;
my $Config = Config::Config->new;
my $dbh = DBI->connect($Config->{'db_source'}, $Config->{'db_username'}, $Config->{'db_password'});
if (! $dbh) { $@ = DBI->errstr; return undef; }
my $sth = $dbh->prepare('delete from Accounts where (id = ?)');
if (! $sth) { $@ = DBI->errstr; return undef; }
my $res = $sth->execute($self->{'id'});
if (! $res) { $@ = DBI->errstr; return undef; }
$sth->finish;
$dbh->disconnect;
return 1;
}
my $Account = Accounts::Account->new(id => $form{'id'});
print "$Account->{'Company'}";
$Account->Delete();