Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 24th, 2012  |  syntax: None  |  size: 1.29 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Perl to PHP MySQL OOP example [closed]
  2. sub new {
  3.   my $class = shift;
  4.   my $Config = Config::Config->new;
  5.   my %params = @_;
  6.  
  7.   my $key = undef; my $value = undef;
  8.   foreach my $tmp (keys %params) { $key = $tmp; $value = $params{$key}; }
  9.   if (! $key) { return undef; }
  10.  
  11.   my $dbh = DBI->connect($Config->{'db_source'}, $Config->{'db_username'}, $Config->{'db_password'}) or return undef;
  12.   my $sth = $dbh->prepare("select * from Accounts where ($key = ?) limit 1") or return undef;
  13.   my $res = $sth->execute($value) or return undef;
  14.  
  15.   my %Data;
  16.   while (my $hashref = $sth->fetchrow_hashref) { %Data = %{$hashref}; }
  17.   $sth->finish; $dbh->disconnect;
  18.  
  19.   if (! %Data) { return undef; }
  20.   bless { %Data }, $class;
  21. }
  22.  
  23. sub Delete {
  24.   my $self = shift;
  25.   my $Config = Config::Config->new;
  26.  
  27.   my $dbh = DBI->connect($Config->{'db_source'}, $Config->{'db_username'}, $Config->{'db_password'});
  28.   if (! $dbh) { $@ = DBI->errstr; return undef; }
  29.  
  30.   my $sth = $dbh->prepare('delete from Accounts where (id = ?)');
  31.   if (! $sth) { $@ = DBI->errstr; return undef; }
  32.  
  33.   my $res = $sth->execute($self->{'id'});
  34.   if (! $res) { $@ = DBI->errstr; return undef; }
  35.  
  36.   $sth->finish;
  37.   $dbh->disconnect;
  38.  
  39.   return 1;
  40. }
  41.        
  42. my $Account = Accounts::Account->new(id => $form{'id'});
  43.        
  44. print "$Account->{'Company'}";
  45.        
  46. $Account->Delete();