Link to home
Start Free TrialLog in
Avatar of Rusty20009
Rusty20009

asked on

Pulling MySQL for calculation using PHP

This is the PHP that I have so far to display firstname, lastname, birthdate and email for a MySQL database that I have. I want to know now how to make the calculation for age, I have the SQL ready (see $age_mysql) but do not know how to send it to the database to get the calculated age in return displayed in the HTML page.

Also, is there an easy way to get the dates displayed other than in MySQL format YYYY-MM-DD?


This is my program to-date:

mysql_select_db($dbname, $DBConn);
$query_mysql = "SELECT * FROM birthdays";
$mysql = mysql_query($query_mysql , $DBConn) or die(mysql_error());
$row_mysql = mysql_fetch_assoc($mysql );
$totalRows_mysql = mysql_num_rows($mysql );
$age_mysql = "(YEAR(CURDATE())-YEAR(birthdate)) - (RIGHT(CURDATE(),5)<RIGHT(birthdate,5))";
?>

<html>
<head>
<title>Birthdays</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<table width=75%>
<tr>
<td width=20%>Last Name</td>
<td width=20%>First Name</td>
<td width=20%>Date Of Birth</td>
<td width=20%>Email</td>
<td width=20%>Age</td>
</tr>
<?php do { //Begin the loop to iterate through the records ?>
<tr>
<td><?php echo $row_mysql['lastname']; ?></td>
<td><?php echo $row_mysql['firstname']; ?></td>
<td><?php echo $row_mysql['birthdate']; ?></td>
<td><?php echo $row_mysql['email']; ?></td>
<td>the age calculation to be inserted here</td>
</tr>
<? } while ($row_mysql = mysql_fetch_assoc($mysql )); //End the loop ?>
</table>
</body>
<?php
mysql_free_result($mysql);
//And close the connection
mysql_close($DBConn);
?>
SOLUTION
Avatar of CosminB
CosminB

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CosminB
CosminB

and if you want to find the age try this:
<?php
$dateString = '1980-03-21';
$age = date('Y', time()-strtotime($dateString))-1970;
echo $age;
?>