Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^9: Making an automatic counter adder for all webpages

by polettix (Vicar)
on Dec 24, 2007 at 12:45 UTC ( #658875=note: print w/ replies, xml ) Need Help??


in reply to Re^8: Making an automatic counter adder for all webpages
in thread Making an automatic counter adder for all webpages

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?


Comment on Re^9: Making an automatic counter adder for all webpages
Select or Download Code
Re^10: Making an automatic counter adder for all webpages
by Nik on Dec 27, 2007 at 21:55 UTC
    Thank you once again for the explanation! I still though have some unclear parts which i dont understand.

    When i do this:

    my $counter = 'SELECT pagecounter FROM counters WHERE pagename = ?' +, undef, $pagename;
    I'm actually expecting one single item for return and NOT an entire array. For example iam expecting 1 filed only (pagecounter that is) that will hold lest say number 75. This is one number so it seems logical to me to store it on a single variable, so i dont get why the baove aint working.

    In your response you are saying and semantically (assigning that list to the $counter variable isn't going to help you get the counter from the DB, is it?): Why you say list? i think iam expecting an item only not a bunch of items(list)

    Also:

    my ($counter) = $db->selectrow_array( 'SELECT pagecounter FROM counters WHERE pagename = ?', undef, $pagename);
    If for some reason we expect an array why doesn't the following code work as well? I'm considering that 'list' and 'array' are the same thing, both are a bunch of values, right?:
    my @counter = $db->selectrow_array( 'SELECT pagecounter FROM counters WHERE pagename = ?', undef, $pagename);
    What exactly is the difference between ($counter) and @counter ?

    And the last thing is that i dont understand although you tried to expalin it to me the use of method 'selectrow_array'. Is it really necessary to tell it we are grabbing an array? doesn't the 'select' do this by its own?

    As you can see i'am really confused....

    ps. When i want to print variable counter i do 'print $counter' or 'print ($counter)' ?

      You should really consider getting some basic text about Perl. Anyway...

      For the second time, this is plain wrong:

      my $counter = 'SELECT pagecounter FROM counters WHERE pagename = ?', undef, $pagename;
      Where is $db? Why do you think that these is going to get you the page counter from the DB? Did you actually try this? What was the result? What ended up in $counter? I think you're not doing your part of the work here, because you continue do discuss without trying anything. There's a time for theory, and a time for practice. And you're not practicing.

      Second, I really don't understand why you're so reluctant to use my solution. You're trying all possible variations, both working and not, and I simply don't understand why. I usually try to understand if and why a solution works, before proposing alternatives and asking for a discussion on them.

      Third, there's no function in the DBI interface that lets you extract a single item from the database. None I am aware of, anyway. The closest one is the extraction of an array/list, so we have to stick to it. To be fair, even if it's called selectrow_array, it actually returns a LIST. You can read in the documentation what the difference is. For our case, it's certainly true that the following will both work:

      # A LIST assigned to a LIST my ($counter) = something_that_returns_a_LIST; # A LIST assigned to an ARRAY my @counter = something_that_returns_a_LIST;
      when we know in advance that the returned list will contain only 1 element. I simply prefer the former, because it puts the desired value directly into the scalar variable $counter. This is not the case of the second approach, that obliges you to get $counter[0] to take the real value of the counter. Some hours on perldata seem appropriate.

      Regarding the use of the parentheses with print, you can do whatever you want. I tend to follow the advice in "Perl Best Practices" and avoid parentheses with the built-in functions and operators, but you can decide to always use parentheses in your code, and we'll all be happy.

      Be sure to do your hard work before returning for more explainations. It's ok to be confused, but if you don't do anything, not even try what you write, you're just giving me the impression that you value your time much more than mine. I'm not going to be so tolerant the next time.

      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?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://658875]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2014-07-25 11:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (170 votes), past polls