doumu6941 2016-04-21 17:20
浏览 44
已采纳

从mysql中比较PHP中的两个日期

In MySQL I have two dates: birth_date as Date of birth and date_starb as Date of death. From the table all people I need select only the dead people and age of the deceased:

$sql = "SELECT members.*, TIMESTAMPDIFF(YEAR,'birth_date','date_starb') AS age FROM members WHERE (date_starb != 0)"; //select only dead peoples

query processing:

<?php
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Fetch a result row as an associative array
while($row = $result->fetch_assoc()) {
?>
<td><?php echo $row["name"];?></td>
<td><?php echo date("j. F Y", strtotime($row["birth_date"]));?></td>
<td><?php echo date("j. F Y", strtotime($row["date_starb"]));?></td>
<td><?php echo $row["age"];?></td>
<?php
}
}
else
{
echo "0 results";
}
$conn->close();
?>

As in result, I get the correct number of lines, names and dates of birth and death, but in the column "age" I have no result. Nor did the page I do not see an error message.

Some solution? Thanks for answer.

  • 写回答

1条回答 默认 最新

  • dongpiao9078 2016-04-21 17:23
    关注

    You don't need to wrap field names in '

    $sql = "SELECT members.*, TIMESTAMPDIFF(YEAR,birth_date,date_starb) AS age FROM members WHERE (date_starb != 0)"; //select only dead peoples
    

    otherwise MySQL treats them like a strings

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?