Hi, I'm just having a play around to build my own SQL.pm file, which would let me have easy access to running SQL commands (without having to do long winded DBI coding). This isn't a necessity, but more of a "project" I just wanted to do, to get a bit of experience with writing my own OO module. Here is what I have in AndySQL.pm:
#!/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;
Then in test.cgi:
#!/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";
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

In reply to OO Coding - way to make it "save" the values? by ultranerds

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.