Peter Schoeller Peter Schoeller - 1 year ago 89
SQL Question

compare two date in php from mysql

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:

$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>
echo "0 results";

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.

Answer Source

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