Then in test.cgi:#!/usr/bin/perl use strict; use warnings; use DBI; use DBD::mysql; package AndySQL; sub new { my $self = {}; bless $self, "AndySQL"; my ($ignore,$user,$pass,$host,$port,$database,$debug) = @_; $self->{connect} = DBI->connect("dbi:mysql:$database:localhost:33 +06", $user, $pass) || die $DBI::errstr; $self->{debug} = $debug; return $self; } sub disconnect { } sub table { my ($self,$table) = @_; $self->{table} = $table; return $self; } sub select_options { my ($self,$opts) = @_; $self->{select_options} = $opts; return $self; } sub count { my ($self,$args) = @_; $self->{table} || die "No table defined!"; my $select_options = $self->{select_options} || ''; my @cond_vals; my @cond_fields; if (ref $args eq "HASH") { map { push @cond_fields, "$_ = ?"; push @cond_vals, $args->{$_}; } keys %$args; } my ($cond_string,$query); if ($cond_fields[0]) { $cond_string = "WHERE " . join " AND ", @cond_fields; $query = qq|SELECT COUNT(*) FROM $self->{table} $cond_string $ +select_options|; } else { $query = qq|SELECT COUNT(*) FROM $self->{table} $select_option +s|; } print "Query: $query \n" if $self->{debug}; my $sth = $self->{connect}->prepare($query); $sth->execute(@cond_vals); my $total = $sth->fetchrow() || 0; $sth->finish(); return $total; } sub select { } sub delete { } sub do_query { } 1;
This works to a point. The first 2 run ok, but then when I go back and try to use $tbl, the "table" gets reset back to the first one ("Page_Counter"). How can I make it so that I can re-use the Page_Counter table, even after setting a new value in the 2nd function? Hopefully that makes sense ;) TIA! Andy#!/usr/bin/perl use strict; use warnings; use AndySQL; # CONFIG VARIABLES my $database = "blog_counter"; my $host = "localhost"; my $port = "3306"; my $user = "dev"; my $pass = "123foobar84!?"; my $DB = AndySQL->new($user,$pass,$host,$port,$database,1); my $tbl = $DB->table('Page_Counter'); $tbl->select_options("LIMIT 50"); my $total = $tbl->count(); print "Total found: $total \n"; my $tbl2 = $DB->table('Testing'); $tbl2->select_options("LIMIT 50"); my $total = $tbl2->count(); print "Total found: $total \n"; my $total2 = $tbl->count(); print "Total found: $total2 \n";
In reply to OO Coding - way to make it "save" the values? by ultranerds
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |