I want to get the last date from a table. So I can show the last added or changed item:
$qry = "
Select
MAX(changedate)
FROM
producten
";
$stmt = $connection->prepare($qry);
$stmt->error;
$stmt->execute();
$result = $stmt->get_result();
$up = $result->fetch_assoc();
$stmt->close();
$main .='Last update '.$up['changedate'];
But I receive this error message:
Notice: Undefined index: changedate in /home/jcslnl/domains/jcsl.nl/public_html/michael/paginas/home.php on line 22
Is this because MAX is not longer supported? Or should I continue to search for the error?
It could also be that I did something wrong in the table, it's the firsttime i used timestamp i.s.o. unix timestamp.
The answer to your question is yes, mysqli supports MAX. However, you need to alias the column, since you aren't actually selecting the column changedate
, but the result of a function which selects the value of changedate
. In which case SELECT MAX(changedate) as changedate
provides you with the desired result.
You can also use var_dump($up);
to see this more clearly in your result.
Aggregate functions will work in mysql, different to the sql standard. to access the value of MAX(changedate) from php, you have to alias it: like this
Select
MAX(`changedate`) as cngdate
FROM
your_table
You have to add an alias for a result of MAX function,
SELECT MAX(changedate) AS maxdate
FROM producten
print_r($up)
Why guess is that it is named something different. So just name it in your query
Select max(changedate) as maxdate
And then use $up['maxdate'] in your output.
add $stmt->bind_result($changedate);
after the execute statement. i.e.
$qry = "
Select
MAX(changedate)
FROM
producten
";
$stmt = $connection->prepare($qry);
$stmt->error;
$stmt->execute();
$stmt->bind_result($changedate);
$stmt->close();
$main .='Last update '.$changedate;
The column result will actually be MAX(changedate)
, i.e. $up['MAX(changedate)']
.
However to make it more readable you should use an alias for you selection
Select
MAX(changedate) as changedate
FROM
producten
and use the same $up['changedate']
.