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 7
rgranlundAsked:
Who is Participating?
 
_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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.