http://www.perlmonks.org?node_id=658775


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. 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?

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
    Thank you very much for the detailed explaining, a more simple, less code needed and straightforward approach that the initail if(...) structure i had in mind

    I didn't knew i could pass variables as arguments to $db->do(...) like that, i was under the impression that i only was used when one had a fixed mysql statement with all arguments known (no vars)! If not i was using prepare and execute, i mean in case of unknown variables like '?' for '$pagename'. Can the 'undef' be ommited too as 'statemnt,,@bind_vars' ? The 'undef' is still a mystery to me on what it does although i read the help section.

    Another question is that i didnt understand why you uses this version of select:

    my ($counter) = $db->selectrow_array( 'SELECT pagecounter FROM counters WHERE pagename = ?', undef, $pagename );
    If i wanted to dot he same i would do something like:
    my $counter = 'SELECT pagecounter FROM counters WHERE pagename = ?' +, undef, $pagename );
    or
    my $sth = $db->prepare('SELECT pagecounter FROM counters WHERE pagenam +e = ?'); $sth->execute($pagename);
    Why the need for ($counter) and the method $db->selectrow_array ?
      There's nothing magic in the undef you have to pass to the do method. The required interface when you want to pass bind variables is the following:
      $dbh->do($statement, \%attr, @bind_values);
      The $statement is the query string. \%attr indicates a reference to a hash where you can pass additional attributes (much like the ones you pass to the connect method). Then, you can put a list of values to bind to the various '?' you put into $statement (yes, @bind_values is an array, but it's been put only to indicate that more values are expected here).

      If you don't want to pass additional attributes, you simply put an undef where \%attr is expected. So you end up with something like:

      $dbh->do($statement, undef, @bind_values);
      It's really nothing more than this.

      Regarding the matter of grabbing the counter value, this is plain wrong, both syntactically (you close a paren that you never open) and semantically (assigning that list to the $counter variable isn't going to help you get the counter from the DB, is it?):

      my $counter = 'SELECT pagecounter FROM counters WHERE pagename = ?' +, undef, $pagename );
      On the other hand, this is incomplete:
      my $sth = $db->prepare('SELECT pagecounter FROM counters WHERE pagenam +e = ?'); $sth->execute($pagename);
      because you still need to get the result and put it inside some variable (e.g. with fetch_whatever). My solution merges it all into a single call, and gives you the counter's value directly inside the $counter variable:
      my ($counter) = $db->selectrow_array( 'SELECT pagecounter FROM counters WHERE pagename = ?', undef, $pagename);
      The selectrow_array functions gives you the first row of the result as an array, and we know that at most one record will be given for this query. Moreover, we already know that this array is going to contain one item only (because I'm SELECTing only the pagecounter field), so assigning this array to the list ($counter) simply puts the desired page counter into the $counter variable.

      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?
      A reply falls below the community's threshold of quality. You may see it by logging in.