Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
There is a lot of context that I left out, let's proceed in order.
  • I'm assuming that the counters TABLE is already present in the database, so you don't need to create it every time you want to increase a counter. Creating tables is something that I usually avoid doing inside the code: I create them "offline", then assume that they're there when coding.
  • I usually put a RaiseError => 1 when connecting to the database. This means that any error (e.g. trying to insert a duplicate record which violates the primary key constraint) just dies. To prevent this in this specific case (in which the error is something that I consider "physiological"), I put an eval block around the $db->do(...) statement, which executes the code block but doesn't exit from the program. This is the Perl way to exception handling, you can read all this in eval's documentation.
  • I made a mistake and used a $ instead of ? in the UPDATE query, sorry!
  • The $db->do(...) can often be used to merge the prepare and execute phases into a single call, which can be easier to both write and read. The "extended" interface call is $rv  = $dbh->do($statement, \%attr, @bind_values); (see DBI docs), which means that I'm passing an undef attribute hash reference \%attr. So,
    $db->do('UPDATE counters SET pagecounter = pagecounter + 1 WHERE pagen +ame = ?', undef, $pagename);
    is equivalent to
    my $sth = $db->prepare( 'UPDATE counters SET pagecounter = pagecounter + 1 WHERE pagename = + ?'); $sth->execute($pagename);
  • Regarding the pagename value, what I'm saying is that you can use the full name instead of chopping ".html" out. As long as different pages have different names, and the same page has the same name, you can use whatever you like to name them inside the table.
  • After the two lines of code to increase the counter, you can grab the counter directly from the database, with a simple query:
    my ($counter) = $db->selectrow_array('SELECT pagecounter FROM counters + WHERE pagename = ?', undef, $pagename);
    As you can see, there's a lot to read in the DBI documentation ;)
Summing it up:
sub increase_pagecount_for { my ($db, $pagename) = @_; eval { # Just ignore errors if the record already exists $db->do('INSERT INTO counters (pagename, pagecounter) VALUES (?, + 0)', undef, $pagename); }; $db->do( 'UPDATE counters SET pagecounter = pagecounter + 1 ' . ' WHERE pagename = ?', undef, $pagename ); my ($counter) = $db->selectrow_array( 'SELECT pagecounter FROM counters WHERE pagename = ?', undef, $pagename ); return $counter; } ## end sub increase_pagecount_for

Hey! Up to Dec 16, 2007 I was named frodo72, take note of the change! Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Io ho capito... ma tu che hai detto?

In reply to Re^7: Making an automatic counter adder for all webpages by polettix
in thread Making an automatic counter adder for all webpages by Nik

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others imbibing at the Monastery: (1)
    As of 2024-07-16 09:12 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?
      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.