Reputation: 35
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.
Upvotes: 0
Views: 133
Reputation: 4392
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
Upvotes: 2