Reputation: 1351
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.
Upvotes: 0
Views: 11076
Reputation: 57650
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 !=''
Upvotes: 1
Reputation: 74018
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
}
Upvotes: 1
Reputation: 91734
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!=''";
^^^
Upvotes: 5