0

I need to select data from a mysql database from the past 12 months based on the current date. I have the dates saved in the database as unix timestamps but my query does not seem to work.

$query="SELECT user_id, COUNT(first_name) AS member_count
FROM main_user
WHERE renew_date<'$time' AND renew_date>'$old_time' WHERE renew_date!=''";

Basically I need to count all instances of first_name where there is a renew_date timestamp.

1
  • 3
    even your variable names says us that DATE cant be compared with TIME. Commented Dec 5, 2012 at 15:50

4 Answers 4

5

You have an additional WHERE where you should use AND:

$query="SELECT user_id, COUNT(first_name) AS member_count
FROM main_user
WHERE renew_date<'$time' AND renew_date>'$old_time' AND renew_date!=''";
                                                    ^^^
Sign up to request clarification or add additional context in comments.

Comments

3

You have an error in your query, you have two WHERE clauses!

Comments

1

You can find this and other errors, when you test the return value from your query

$query = 'select ...';
$result = $mysqli->query($query);
if ($result === false) {
    // error handling
    echo $mysqli->error;
} else {
    // query successful
    // process result set
}

Comments

1

You put WHERE twice. You can use From_UNIXTIME function in mysql

WHERE FROM_UNIXTIME(renew_date)<NOW() 
    AND FROM_UNIXTIME(renew_date)> (NOW()-INTERVAL 1 year)
    AND renew_date !=''

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.