Re: too many records

From: Date: Thu, 15 Jun 2000 01:42:37 +0000
Subject: Re: too many records
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-424@lists.php.net to get a copy of this message
At 09:13 PM 6/14/00 +0000, Ken N wrote:
Hi,
        I am working with a database that has two fields, an IP and a
date. This is a snippet of the data, +-----------------+---------------------+ | 63.91.195.171 | 2000-04-22 17:08:09 |
| 64.14.95.137    | 2000-04-27 01:57:07 |
| 64.20.27.92     | 2000-04-06 16:20:09 |
+-----------------+---------------------+ What I wnat to do is select all the records from a certian month, I wrote a for loop that looks like this. for ($i = 0; $i < 12; $i++) { $next_month = $i + 2; $month_query = "SELECT * FROM verbscom WHERE date > '2000-$i-31' and date < '2000-$next_month-00'"; $month_total = mysql_query($month_query); $total_hits_month = mysql_num_rows($month_total); $month = $i + 1; echo( TR ); echo( TD . "Total hits for this month" . $month . ":" . CLOSE_TD ); echo( TD . $total_hits_month . CLOSE_TD ); echo( CLOSE_TR ); } Now, it seems to work fine except for if I do SELECT * FROM verbscom I get a few more rows than if I add the totals from the 12 querys this loop executes. If anyone has a better idea of how I might want to approach this subject I would be willing to give it a shot. As far as I can tell this loop should do the trick. But I am a little confused
Hi Ken, It looks like you have the right approach. The only changes that I would make to this code would be: /* Start your for counter at 1 - this way you can avoid needing to add one to your counter to find the current month */ for ($month = 1; $month < 13; ++$month) {
        /* Handle the change from December to January */
        $next_month = ($month % 12) + 1;
        /* If the next month is January, then change the year */
        if ($next_month > 1)
                $next_year = $year;
        else
                $next_year = $year + 1;
        /* Use count to find the number of hits.
           In this case, MySQL allows you to omit the days from your date */
        $month_query = "SELECT count (IP) FROM verbscom
                WHERE date BETWEEN '2000-$month'
                AND '$next_year-$next_month'";
        $query_handle = mysql_query ($month_query);
        $total_hits_month = mysql_result ($query_handle, 0, 0);
        echo( TR );
        echo( TD ."Total hits for month $month:". CLOSE_TD );
        echo( TD . $total_hits_month . CLOSE_TD );
        echo( CLOSE_TR );
} HTH, Zak Greant Creative Director Nucleus Information Service Inc. "I am rather like a mosquito in a nudist camp: I know what I ought to do, but I don't know where to begin." - Stephen Bayne

« previous php.db (#424) next »