schnarff has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a large database-driven web site, one of whose components is a hit tracker that will update a table each time a link gets clicked.

I've set the whole thing up with DBI/MySQL, and most of it works great. However, the actual update query to post the new number of hits, the IP they're from, etc., is frustrating me greatly.

To properly fit things in the database, I'm first making up a string that contains my SQL statement. The string ends up looking like this:
UPADTE urls SET hits = "?", unixdate = "?", ip1 = "?" WHERE id = ?
After filling up an array with the appropriate values -- and yes, I'm checking that the values are in the right order, and of the right number -- I call this code:
$sth = $dbh->prepare($stmt); #$stmt is, of course, my string $sth->execute(@values); my $tmp = $sth->rows; print "Rows Affected: $rows"; #Just to see if it worked.
Amazingly enough, nothing is printing (I'd earlier printed out my http headers and other data, so it's not that I can't print altogether) at all on this. Meanwhile, my database does not reflect the update at all.

Is there any good reason, based on this code, that my query wouldn't work? I'll be happy to provide more info and more surrounding code if it helps.

Alex Kirk

Replies are listed 'Best First'.
Re: DBI Update Query Not Updating
by abstracts (Hermit) on Mar 16, 2002 at 03:20 UTC
    Hello

    First of all, if nothing is printed at all, you should check your server's error logs (Hope you used -w to catch warnings). Second, use CGI::Carp 'fatalsToBrowser'; to send the errors to the browser so that you can see them. Third: you have a spelling mistake (UPADTE?) and extra quotes in your statement:

    UPDATE urls SET hits = ?, unixdate = ?, ip1 = ? WHERE id = ?
    Also, as the number of hits increase, how are you squeezing the ips and dates into the table? And do you really want these in the table? Just wondering.

    Hope this helps,,,

    Aziz,,,

Re: DBI Update Query Not Updating
by metadatum (Scribe) on Mar 16, 2002 at 04:49 UTC
    What about error checking after each DBI call? or do you have a generic DBI error handler?

      Just use the "RaiseError" option in the DBI constructor and you'll automatically have a DBI error handler on all statements, with no work whatsoever.

      -Any sufficiently advanced technology is
      indistinguishable from doubletalk.

Re: DBI Update Query Not Updating
by seattlejohn (Deacon) on Mar 17, 2002 at 19:21 UTC
    This may not be the source of your problem, but I've had trouble getting the rows method to return meaningful results in MySQL...