0

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
  • 1
    remove the single quotes around birth_date and date_starb Commented Apr 21, 2016 at 17:23

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.