Query a Time stamped table For Current Month

I want to query a DB timestamped table and get the ID's of every entry from the current month.

What I have, that is not working is this:
<?php
            function getMonth() {
    global $wpdb;
    $getMonth = $wpdb->get_results("SELECT id, FROM_UNIXTIME(date_added) timestamp 
  FROM wp_connections
 WHERE 'date_added' >= UNIX_TIMESTAMP(LAST_DAY(CURDATE()) + INTERVAL 1 DAY - INTERVAL 1 MONTH)
   AND 'date_added' <  UNIX_TIMESTAMP(LAST_DAY(CURDATE()) + INTERVAL 1 DAY");
    
    
    foreach($getMonth as $row) {
        $id = $row->id;
        echo '<h2>MONTH ID'.$id.'</h2>';
    }
}

Open in new window

time_stamp.PNG
LVL 8
rgranlundAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_agx_Commented:
Get rid of the single quotes around column names. Otherwise you're only comparing the literal characters "date_added", not the values inside that column, which is why it doesn't work. This does:

SELECT id, FROM_UNIXTIME(date_added) timestamp 
  FROM wp_connections
 WHERE date_added >= UNIX_TIMESTAMP(LAST_DAY(CURDATE()) + INTERVAL 1 DAY - INTERVAL 1 MONTH)
   AND date_added <  UNIX_TIMESTAMP(LAST_DAY(CURDATE()) + INTERVAL 1 DAY)

Open in new window


Edit: While it's not needed here, if you need to escape a column name, use a backtick (not the same as single quote).
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gr8gonzoConsultantCommented:
On a side note (no points for this), tour function name "getMonth" could be improved. That name implies you will get back a "month" as a value, when you are really getting back connection IDs for a given month. Always try to name your functions in a way that explains what the user will get back like "getConnectionIDsForMonth". It might be longer to type but it's absolutely clear what you can expect to get back.

One more thing - this might just be because you're debugging but your function doesn't currently return anything. It -echoes- the content directly to the output but functions should ideally return content so that the calling code can decide what to do with it (e.g. display it or maybe save it to a file or email it etc....)
2
rgranlundAuthor Commented:
It works now!  Thanks Also, is that the best way to return the value?  Instead of ECHO how do I RETURN?
function getMonth() {
                    ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
                    global $wpdb;
                    echo '<u><h2>Current Month</h2></u><br />';
                    $getMonth = $wpdb->get_results("SELECT id
  FROM wp_connections
 WHERE date_added >= UNIX_TIMESTAMP(LAST_DAY(CURDATE()) + INTERVAL 1 DAY - INTERVAL 1 MONTH)
   AND date_added <  UNIX_TIMESTAMP(LAST_DAY(CURDATE()) + INTERVAL 1 DAY)");


                    foreach ($getMonth as $row) {
                        $id = $row->id;
                        return '<h2>MONTH ID' . $id . '</h2>';
                    }
                }

                getMonth();

Open in new window

?
0
_agx_Commented:
Sorry, I don't really know php. Maybe gr8gonzo can assist?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.