in reply to Re^6: Making an automatic counter adder for all webpages
in thread Making an automatic counter adder for all webpages
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,
is equivalent to$db->do('UPDATE counters SET pagecounter = pagecounter + 1 WHERE pagen +ame = ?', undef, $pagename);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:
As you can see, there's a lot to read in the DBI documentation ;)my ($counter) = $db->selectrow_array('SELECT pagecounter FROM counters + WHERE pagename = ?', undef, $pagename);
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^8: Making an automatic counter adder for all webpages
by Nik (Initiate) on Dec 24, 2007 at 10:34 UTC | |
by polettix (Vicar) on Dec 24, 2007 at 12:45 UTC | |
|
In Section
Seekers of Perl Wisdom