phpmysqlmax

get(column_name) after query(SELECT MAX(column_name)...) doesn't return the MAX value


I need to fetch the highest value from a mysql db column. It's a timestamp. Using mysql MAX function it's not working, bellow you can see the code:

//Create new db object
$db = new Db();

//Query
$qr_changelog = $db->query("
    SELECT MAX(log_datetime) FROM ca_change_log
");

//Fetch result
$last_change = $qr_changelog->get('log_datetime');

//Print result
echo "Last Change:" . $last_change;

If I run the query without MAX function it works (but obviously returns all values). If I run the query from mysql server console it works. Apache log shows no errors.

I also tried with mysqli:

$db1 = new mysqli('localhost', 'user', 'passwd', 'db');

if ($db1->connect_errno > 0) {
    die('Unable to connect to database [' . $db1->connect_error . ']');
}

$qr_changelog = $db1->query("
    SELECT MAX(log_datetime) FROM ca_change_log");

while ($row = $qr_changelog->fetch_assoc()) {
    echo $row['log_datetime'] . '<br />';
}

Any ideas?


Solution

  • You can order the SQL and limite the value if You Sant the max value. Something like this: SELECT log_datetime FROM ca_change_log order by log_datetime dec limite (1).