Jump to content

Recommended Posts

If there is an issue, it was in the import. I have pull down selections for the m/d/y to avoid stupid entries, but who knows with the import. I'll look into that.

 

Question then: If I search for "NULL" records (to start this)... I have ROWS of NULL  in all fields. I should delete those to clean things up first, right?

 

Is there then an easy way to look for entries in Cbirthdate, say beyond 60 years old that I can pick through?

<?php include_once("security/SECsecurity.php"); ?>
<?php $DOCROOT = $_SERVER['DOCUMENT_ROOT'] ; ?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>DB info</TITLE>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="GOOGLEBOT" CONTENT="NOARCHIVE">
    <META NAME="ROBOTS" CONTENT="NONE">
    <LINK REL="stylesheet" TYPE="text/css" HREF="text.css">
</HEAD>
<BODY>
<a name="top"></a>
<center>
<?php echo "<A name=\"top\"></A><b><center><H1>\"<I>Listener Info</I>\":</H1>"; ?>
<div style='width:100%; background-color:silver; text-align:right'>
  <?php SECShowAdminLink(); ?>
   
  <?php SECShowLogoutLink(); ?>  <A href="data.php">Main Data Page</A>  
</div>
<?php
include("dbinfo.inc.php");
mysql_connect(mysql,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

////// (edited) Code from Styles2304 starts here

$query = "SELECT Cbirthdate FROM birthdays ORDER BY Cbirthdate LIMIT 1";
<?php
//Todays Date
$TDate = date("Y-m-d");

//Other Variables (in order) Age, Lowest Age, Highest Age, Average Age, Total of Ages, and Number of Birthdays.
$Age = 0;
$LAge = 0;
$HAge = 0;
$AAge = 0;
$ATotal = 0;
$BNumber = 0;

//Finds the Lowest Age
$query = "SELECT Cbirthdate FROM birthdays ORDER BY Cbirthdate LIMIT 1";

$result = mysql_query($query)
            or die(mysql_error());
            
while ($row = mysql_fetch_array($result)) {
////////////Old Code:  $Bbirthdate = $row['Cbirthdate']; With your changed should read:
$Cbirthdate = $row['Cbirthdate'];
}
$LAge = $TDate - $Cbirthdate;

echo 'The Lowest Age is: ' . $LAge . '<br>';

//Finds The Highest Age
$query = "SELECT Cbirthdate FROM birthdays ORDER BY Cbirthdate DESC LIMIT 1";

$result = mysql_query($query)
            or die(mysql_error());
            
while ($row = mysql_fetch_array($result)) {
//////////Old Code: $Bbirthdate = $row['Cbirthdate']; With changes should read:
$Cbirthdate = $row['Cbirthdate'];
}

$HAge = $TDate - $Cbirthdate;

echo 'The Highest Age is: ' . $HAge . '<br>';


//Finds the Average Age
$query = "SELECT Cbirthdate FROM birthdays ORDER BY Cbirthdate";

$result = mysql_query($query)
            or die(mysql_error());
            
while ($row = mysql_fetch_array($result)) {
///////// Old Code: $Bbirthdate = $row['Cbirthdate']; With changes should read:
  $Cbirthdate = $row['Cbirthdate'];
  $Age = $TDate - $Cbirthdate;

  $ATotal = $ATotal + $Age;
  $BNumber++;
}
  $AAge = $ATotal / $BNumber;

echo 'And The Average Age is: ' . $AAge;

?>
<BR><BR><BR><P><A href=\"#top\">Back to top</A></P></B><BR>
<BR><BR><BR>
           <?php include "include/footer.php"; ?>	
	  <a name="bottom"></a>
</BODY>
</html>

 

If you wouldn't mind . . . would you give my code a shot again and at least give me the satisfaction of knowing if it works? lol hope this helps either way!

I noticed, that if there were invalid dates (birth dates) that it would still add them to the total...

 

so say there are 4 entries in the database

 

and one of the entries is invalid: 1966-45-23

 

since 45 isn't a actual month, it will still say it found 4 entries.

 

so then it is taking the four entries and dividing them by the sum of the 3 valid entries, giving an invalid average.

 

does that make sense?

 

So... I would check to make sure all the entries are valid

Sure wish they allowed a longer period of time for edits....

 

I tried the "valid rows" script. I get a bunch of returns, but they all say, "Valid" and there is no row # return. Here's what I entered:

<?php
include("dbinfo.inc.php");
mysql_connect(mysql,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
    $sql = mysql_query("SELECT * from birthdays");
    while($row = mysql_fetch_array($sql)){
        list($y,$m,$d) = explode("-",$row['birth']);
        if($m > 12){
            echo '<span style="color:red">Invalid Row: '.$row['birth'].'</span><br>';
        }elseif($d > 31){
            echo '<span style="color:red">Invalid Row: '.$row['birth'].'</span><br>';
        }else{
            echo '<span style="color:green">Valid Row: '.$row['birth'].'</span><br>';
        }
    }
?>

 

I'm not sure where your "birth" works from my fields. Is that the problem?

LG: NP. I just wasn't sure. Well the good news is that I have no "Invalid" records per your test. I do have a bunch of blanks.

 

How can I modify that code to return the record number with the rows? I don't see a counter.

 

And/or... modify it to JUST return the rows with NO value in that field?

Here's an alternative

 

Run this SQL script

DELIMITER $$

DROP FUNCTION IF EXISTS `yourDBname`.`fnAge`$$
CREATE FUNCTION `yourDBname`.`fnAge`(dob DATE) RETURNS int
BEGIN
      DECLARE a int;
      SET a = YEAR(CURDATE()) - YEAR(dob);
      SET a = CASE DATE_FORMAT(CURDATE(),'%m%d') < DATE_FORMAT(dob,'%m%d')
        WHEN true THEN a-1 ELSE a END ;
      RETURN a;
END$$

DELIMITER ;

 

Then run this query with your column and table name

SELECT MIN(fnAge(adate)) as minage,
  MAX(fnAge(adate)) as maxage,
  AVG(fnAge(adate)) as avgage
  FROM `dates`

It creates a MySQL user function "fnAge(dob)" which returns the age from the dob passed to it. So if you

 

SELECT fnAge ('1949-01-22')

 

it returns "59"; (so it works for dates outside the UNIX epoch ie 1970 - 2037)

 

As the function converts dates to ages, the single query returns the min age, max age and average age for you.

Thanks for clarifying! Just for kicks then.... back to the original question that we've now rabbit-trailed off at least 54 times....

 

How can I put this in my page code to get me the

Lowest age:

Average age:

Oldest age:

 

I cant put "SELECT fnAge ('1949-01-22')" in my query can I? That would always return the same thing. I can connect to the table. What would be next then?

SELECT MIN(fnAge(adate)) as minage,
  MAX(fnAge(adate)) as maxage,
  AVG(fnAge(adate)) as avgage
  FROM `dates`

 

EDIT: (assuming you defined the fnAge function)

<?php
$sql = "SELECT MIN(fnAge(CBirthdate)) as minage,
        MAX(fnAge(CBirthdate)) as maxage,
        AVG(fnAge(CBirthdate)) as avgage
        FROM `birhdays` ";
$res = mysql_query($sql);
list ($min, $max, $avg) = mysql_fetch_row($res);
echo "Min age : $min<br>Max age : $max<br>Average : $avg";        
?>  

Hoky-doky. I tried creating the MySQL function. I went to the "SQL" tab in PHPmyAdmin.

(The Yahoo server I'm working on (no choice) is: phpMyAdmin 2.6.3)

Once in the SQL tab, I typed in your code with changes: (My db name is "contacts", table is "birthdays" and the field I need to work on is "Cbirthdate" -for the record)

DELIMITER $$

DROP FUNCTION IF EXISTS `contacts`.`fnAge`$$
CREATE FUNCTION `contacts`.`fnAge`(dob DATE) RETURNS int
BEGIN
      DECLARE a int;
      SET a = YEAR(CURDATE()) - YEAR(dob);
      SET a = CASE DATE_FORMAT(CURDATE(),'%m%d') < DATE_FORMAT(dob,'%m%d')
        WHEN true THEN a-1 ELSE a END ;
      RETURN a;
END$$

DELIMITER ;

 

However PHPmyAdmin says:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER $$

 

DROP FUNCTION IF EXISTS `contacts`.`fnAge`$$

CR

 

??

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.