[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Pulling MySQL for calculation using PHP

Posted on 2004-03-22
3
Medium Priority
?
450 Views
Last Modified: 2013-12-12
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);
?>
0
Comment
Question by:Rusty20009
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Assisted Solution

by:CosminB
CosminB earned 100 total points
ID: 10648610
you could use the date_format function from mysql
or you could format it using php, using something like this:
<?
$dateString = '2004-03-21';
echo date('r', strtotime($dateString));
?>

so you birthdate line should look like this:

<td><?php echo date('f D Y', strtotime($row_mysql['birthdate'])); ?></td>

look at http://www.php.net/date for more info on date()
0
 
LVL 8

Accepted Solution

by:
william_jwd earned 300 total points
ID: 10648727
try this,

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

<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><?php echo $row_mysql['age']; ?></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);
?>
0
 
LVL 6

Expert Comment

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question