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

  • Home
  • SEARCH
  • 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 7441147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:57:53+00:00 2026-05-29T10:57:53+00:00

I have this query $query = mysql_query (SELECT type, count(*), date FROM tracking WHERE

  • 0

I have this query

$query = mysql_query ("SELECT type, count(*), date FROM tracking WHERE htcode='$htG' AND type IN ('viewed', 'shared', 'printed', 'emailed', 'used') GROUP BY type, date ORDER BY date ASC");

This code:

while ($result = mysql_fetch_assoc($query)){
echo $result['date'] .' / ' .$result['type'].' = ' .$result['count(*)'];
echo '<br>';
}

gives me :

2012-02-01 / viewed = 2
2012-02-03 / emailed = 1
2012-02-04 / shared = 1
2012-02-05 / viewed = 1
2012-02-07 / viewed = 2
2012-02-07 / shared = 3
2012-02-07 / emailed = 1
2012-02-07 / printed = 1

How can i take the query array and pull all (viewed, shared, emailed, printed, used) for today, for yesterday, and for a date range (last 30 days for example)?

  • 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-29T10:57:54+00:00Added an answer on May 29, 2026 at 10:57 am

    To get all results for a specific date range, don’t group by date; instead use a WHERE clause to limit the date of the count. Otherwise the query is very similar.

    $query = mysql_query("SELECT type, count(*) FROM tracking WHERE htcode = '$htG' AND type IN ('viewed', 'shared', 'printed', 'emailed', 'used') AND date >= '$start_date' AND date <= '$end_date' GROUP BY type");
    

    Here’s just the query itself formatted over several lines for clarity:

    SELECT type, count(*)
    FROM tracking
    WHERE htcode = '$htG'
      AND type IN ('viewed', 'shared', 'printed', 'emailed', 'used')
      AND date >= '$start_date' AND date <= '$end_date'
    GROUP BY type
    

    Just make sure you set the values for $start_date and $end_date first. For example, for today you’d just set them both to this:

    $start_date = date('Y-m-d');
    $end_date = date('Y-m-d');
    

    Note that date('Y-m-d') is just today’s date in a format like YYYY-MM-DD.

    For yesterday, you’d pass in yesterday’s timestamp into the date() function, but use the same format:

    $start_date = date('Y-m-d', strtotime('yesterday'));
    $end_date = date('Y-m-d', strtotime('yesterday'));
    

    And for the past thirty days, you’d start 30 days ago and end today:

    $start_date = date('Y-m-d', strtotime('30 days ago'));
    $end_date = date('Y-m-d');
    

    You can do it like this for any start/end date. Note that I’m using strtotime(), which lets you use a lot of English phrases to specify timestamps rather than having to do date arithmetic.

    Also, since you will be doing the query multiple times, it would probably make sense to separate out the logic into its own function:

    function report_range($htG, $start_date, $end_date) {
        $query = mysql_query("SELECT type, count(*) FROM tracking WHERE htcode = '$htG' AND type IN ('viewed', 'shared', 'printed', 'emailed', 'used') AND date >= '$start_date' AND date <= '$end_date' GROUP BY type");
    
        while ($result = mysql_fetch_assoc($query)){
            echo $start_date . '-' . $end_date . ' / ' . $result['type'] . ' = '  . $result['count(*)'];
            echo '<br>';
        }
    }
    

    Then run it with something like this (last 30 days in this example):

    report_range($htG, date('Y-m-d', strtotime('30 days ago')), date('Y-m-d'));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a query that goes like this: $get_feed_amount = mysql_query( SELECT COUNT(*) as
say I have this $result = mysql_query('SELECT views FROM post ORDER BY views ASC');
So i have this script: <?php $query = mysql_query( SELECT meetup AS text, id
I have written this query for retrieving data from mysql as below select FeedbackCode,EMailID,FeedbackDetail,
I have this MySQL query that I'd like to optimize: SELECT min(`topic_id`) FROM `gallery_topics`
If I have this: $results = mysql_query(SELECT * FROM table_name WHERE id=$id); is there
I have following MySQL query: (SELECT c.Channel as name, count(*) as total_episode FROM (
I have a MySQL query like this: SELECT *, SUM(...some SQL removed for brevety)
I have a MySQL query like this: SELECT DAYNAME(CreatedAt) AS TheDay, SUM(Amount) AS TheSum
Background: I have this with rollup query defined in MySQL: SELECT case TRIM(company) when

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.