Sooraj Abbasi Sooraj Abbasi - 4 months ago 7
MySQL Question

I am working on a php project where i want to get the most recent date from my database table

Here is my code: here is a table from which i want to retrieve the most recent payment paid by buyer.

$last_date=date('d-m-Y',strtotime('01-01-2016'));
$query3=mysql_query("SELECT *FROM paid_m_installments WHERE plot_id='$pid' AND buyer_id='$uid'");
while ($rows = mysql_fetch_assoc($query3)) {
$paid_dates =date('d-m-Y',strtotime($paid_dates_r=$rows['install_paid_date']));
if($paid_dates>$last_date){
$last_date=$paid_dates;
}else{
echo "THere is error";
}
}
echo $last_date;

Answer

You just need to get the most recent date by sql, no need to loop result by php:

SELECT STR_TO_DATE(install_paid_date, '%d-%m-%Y') AS install_paid_date 
FROM paid_m_installments
WHERE plot_id='$pid' AND buyer_id='$uid'
ORDER BY install_paid_date DESC
LIMIT 1
Comments