Overflow in Wp-Dstats2 WordPress plugin?

2

There seems to be a problem in the PHP code of the Wp-Dstats2 statistics plug-in for WordPress. The problem occurs when your number of unique visitors (unique IPs) grows large, typically somewhat less that 30.000.

The problem seems to be caused by the fact that the query responsible for the uniques is as follows:

select count(page) as count from wp_dstats2 group by IP;

after which the number of entries in the resultset are counted in PHP. Somehow this does not work when the resultset grows too large.

We can circumvent this nuisance by letting the query find out the exact number we are looking for:

select count(distinct ip) as count from wp_dstats2;

Instead of using the wpdb->get_results() we use the wpdb->get_var(), which leaves us with the following simplified dstats2_unique_reads() function:

function dstats2_unique_reads($range = 'all') {
    global $wpdb, $dstatstable;
    $qstring = "SELECT count(distinct ip) as count FROM $dstatstable";
    if ($range <> 'all') {
        $qstring = $qstring . " where " . dstats2_get_range($range);
    }

    $uniques = $wpdb->get_var($qstring);

    echo $uniques;
}
</>
Share.

About Author

2 Comments

  1. I don’t know if you’ll see this, since it is a fairly old post, but I’ll try.

    I’ve switched to WP 1.5, and wp-dstats2 doesn’t work for it, so started using Stattraq. Amazing result: same overflow problem! If it’s the same issue, the relevant code is:
    `$sqlQuery = “SELECT COUNT(DISTINCT session_id) AS cnt FROM $tablestattraq WHERE ” . (‘all’ != $time_frame ? “access_time BETWEEN ‘$startDate’ AND ‘$endDate’ AND” : ” ) .” user_agent_type=0″;
    $output = $wpdb->get_row($sqlQuery);
    return $output->cnt;

    I’m not facile enough to see why this might cause the same overflow problem–any idea?

  2. Fantastic! I just hit the problem about a week ago, and have been trying to figure it out ever since. Thanks so much for the code!!!