views:

69

answers:

2

I have this code inside of a exit hook in a custom module:

$diff = round(((microtime(true)-$script_start_time)*1000));
watchdog('thaty', $diff);
    if(variable_get('thingy', 0) == 1) {
        $data = array(
           'q'=>$_GET['q'],
           'memory'=>memory_get_peak_usage(),
           'execution_time'=>$diff
        );
        db_query('INSERT INTO {blah} (q,memory,dt,execution_time) VALUES (\'%s\',%b,UNIX_TIMESTAMP(),%b)',$data);
    }

The value of $diff will occasionally consistently get saved in the database as 2147483647, which is the overflow value for an INT, I believe, in PHP. When I log the value via watchdog I can confirm that it does not get set to 2147483647, but the real value, which is a three digit whole number.

Typical values (confirmed from logging):

  • 708
  • 408
  • 413

The execution_time column is int(11).

I can't seem to find the source of this issue. Things I've tried:

+3  A: 

It seems db_query("INSERT....") is deprecated in favour of drupal_write_record().

Try drupal_write_record().

Seeing as this is most certainly a masking problem when building the query (rather than a database problem), maybe that does it better.

Pekka
Thanks, that got it working.
Justin
Using `drupal_write_record` is not a bad idea, but the rest is just wrong. `drupal_write_record`is a convenience function that use db_query itself, which is the standard way of querying in drupal.
googletorp
@googletorp I'm no Drupal expert, but according to the source code, `drupal_write_record` does a number of things to the placeholders before doing the `drupal_query()`. One of these things seems to have fixed the issue. Of course it'd be nicer to look at the query to see what was wrong, but this seems to have worked out
Pekka
@googletorp yes, and that seems to have fixed the OP's issue so it seems to be fine (Although I'd like to understand what did it in the end, the OP stated he used `%d` instead of `%b` before but to no avail).
Pekka
@Pekka, actually it doens't do anything to the placeholders themselves. It just constructs a query very similar to what OP made himself. The difference is that it can use the schema information to fill out default values defined with `hook_schema`, and it will use correct placeholders (%d for INT). Anyways I downvoted you, because you wrote something completely incorrect. `db_query` is the way to create queries and with use of %d, what the OP did should have worked. The real problem could lie in the scheme definition, or OP didn't copy over all the info in the query.
googletorp
@Pekka, Reposted comment as I accidentally submitted it while writing it, and spend too long time formulating it to be able to edit it. See you managed to respond before I completed.
googletorp
@googletorp yeah, no problem. I got the notion that `db_query("INSERT...")` is deprecated from this comment: http://api.drupal.org/api/function/db_query/6#comment-132 if this is incorrect, sorry. I'll edit my answer accordingly
Pekka
@googletorp I stand by the last sentence of my answer, though: If the column is an INT, and the incoming value is `708`, and `%d` is used, and it gets garbled, the problem must be witht the part that builds the query
Pekka
@Pekka: My guess being a seasoned Drupal developer, is that the fault is OP didn't use `%d` or there was some other factor he didn't post. In any regard he got his problem solved and you edited your post (thanks), so I'm good.
googletorp
+1  A: 

This is more of a comment, than an answer, but since I can't do that...

If you have the devel module (http://drupal.org/project/devel) installed, you can use db_queryd(), instead of db_query().

/** * Debugging version of db_query(). * * Echoes the query to the browser. */

oadaeh
Great tip, thanks!
Justin