Trying to select unique years from timestamp column in a table but so far unsuccesful. There are about 50 rows with 3 unique years between them.
$getyears = $dbh->prepare("SELECT DISTINCT DATE_FORMAT(timestamp, '%Y') as timestamp FROM press");
$getyears->execute();
$years = $getyears->fetchAll(PDO::FETCH_ASSOC);
print_r($years);
The result of this is:
Array ( [0] => Array ( [timestamp] => ) )
There should be three different years.
I’m no expert in MySQL and quite new to PDO. Can’t see where I’m going wrong.
Updated
I enclosed timestamp with back ticks but still the same result.
Printing the error info print_r($getyears->errorInfo()); returned:
Array ( [0] => 00000 [1] => [2] => )
timestamp is a int type field and after a double check, the table as a whole definitely contains 3 unique years.
Updated 2
Just a bit more info:
All rows have either of the following in the timestamp field (int(11)):
1230587568
1262123568
1356817968
You can show the error with
or
According to Reserved Words, it is allowed to use
timestampunquoted. Although, it is a good habit to quote it with backticks anyway.date_formatworks with adatetype and not with aninttype. Iftimestampis a unix timestamp (seconds since 1970-01-01), you can tryfrom_unixtimeinstead.