这是我的代码:这是一张表,我想从中检索买家支付的最新款项。
$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;
答案 0 :(得分:0)
你只需要通过sql获取最新的日期,不需要通过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
答案 1 :(得分:0)
您可以使用以下代码找到结果
$query3=mysqli_query("SELECT *FROM paid_m_installments WHERE plot_id='$pid' AND buyer_id='$uid' ORDER BY install_paid_date DESC LIMIT 1");
您必须根据 php版本更改mysql_query函数。对于上面的PHP 5,你应该使用mysqli_query()。
答案 2 :(得分:0)
我的代码正在运行: 最终代码:
$query = mysql_query("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");
if ($query) {
# code...
echo date('d-M-Y',strtotime($last_date=mysql_result($query,0,'install_paid_date')));
}else{
echo mysql_error();
}
注意这里我在Mysql中使用str_to_date()函数,因为我将日期存储为字符串,您可以使用date_fromat()函数,您在MYSQL中使用日期数据类型。