Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6867729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:20:44+00:00 2026-05-27T03:20:44+00:00

I am getting a count of scans in a database. The time field is

  • 0

I am getting a count of scans in a database. The time field is a mysql timestamp (2011-10-20 14:15:12). I have a function that lets me set a timeframe like 30 days, 60 days etc… this was working for weeks. Then I just noticed it broke.

function getScans($timeframe = 0)
{
    if ($timeframe != 0) { 
        $query = 'SELECT COUNT( * ) 
            FROM stats 
            WHERE time <= curdate( )+1 
            AND time >= curdate( )-' . ($timeframe - 1);
    } else {
        $query = 'SELECT COUNT( * ) 
            FROM stats';
    }

    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    return $row[0];
}

I know you have all heard this before but this was completely working last week. I went back today and noticed that when the $timeframe is other than 0, it no longer works. Any help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:20:45+00:00Added an answer on May 27, 2026 at 3:20 am

    The problem is this: curdate( )+1 yields 20111131 (to day 2011-11-30) not what I believe you expect, 2011-12-01, the same is true for the later curdate() call. It’s probably been working fine because earlier in the month the calls have resulted in correct dates and MySQL accepts the formatting, but now it fails to alert on “impossible” dates. Closing in on next month, things start acting up.

    The query should be rewritten like:

    SELECT
      COUNT(*)
    FROM `stats`
    WHERE `stats`.`time` <= DATE_ADD(NOW(), INTERVAL 1 DAY)
      AND `stats`.`time` >= DATE_SUB(NOW(), INTERVAL $timeframe DAY)
    

    You could do something like this:

    function getScans($timeframe = 0)
    {
        if ($timeframe != 0) { 
            $query = 'SELECT COUNT( * ) 
                FROM stats 
                WHERE time <= DATE_ADD(NOW(), INTERVAL 1 DAY) 
                AND time >= DATE_SUB(NOW(), INTERVAL ' . ($timeframe - 1) . ' DAY)';
        } else {
            $query = 'SELECT COUNT( * ) 
                FROM stats';
        }
    
        $result = mysql_query($query);
        $row = mysql_fetch_array($result);
        return $row[0];
    }
    

    But it isn’t the safest approach if $timeframe could be supplied by a user or the like. Rather, you should consider using something like:

    $query = sprintf ('SELECT
      COUNT(*)
    FROM `stats`
    WHERE `stats`.`time` <= DATE_ADD(NOW(), INTERVAL 1 DAY)
      AND `stats`.`time` >= DATE_SUB(NOW(), INTERVAL %d DAY)',
    mysql_real_escape_string ($timeframe, $connection_handle));
    

    or, better yet, search SO for tips on sanitizing user input before adding it to an SQL statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a union query that's getting the count from two different populations. How
I have no problem finding all tags for an object and getting the count
Working with a collection I have the two ways of getting the count of
I'm looking to write a query that grabs a count of a field and
I am trying to figure out the best way of getting the record count
Having trouble with the following segment of code. I'm getting a parameter count mismatch.
The following code seems to be just too much, for getting a single count
We're getting a server error saying Parameter count does not match Parameter Value count.
So I'm getting this error: Error: Column count doesn't match value count at row
I am getting the object reference error in this line: emp[count].emp_id = int.Parse(parts[0]); in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.