Sideshow
Sideshow

Reputation: 1351

MySQL SELECT rows between two values

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

Answers (4)

Shiplu Mokaddim
Shiplu Mokaddim

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

Olaf Dietsche
Olaf Dietsche

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

m4t1t0
m4t1t0

Reputation: 5721

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

Upvotes: 3

jeroen
jeroen

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

Related Questions