基于dateOfBirth mysql时间戳显示年龄的问题

时间:2011-04-09 14:46:28

标签: php mysql smarty

我有一个包含用户个人资料的网站。在主页上,我试图显示每个人的个人资料的摘要。此摘要包括人员姓名,性别和人的年龄。

        <ul>                
            {section name=i loop=$person_list}
                <li><a href="user_detail.php?id={$person_list[i].id}">{$person_list[i].username}, {$person_list[i].gender}, {$age[i]}
            {/section}
        </ul>

我以前使用foreach循环来运行它们,但是我需要索引来显示index.php文件中计算的显示年龄。

if (isset($_GET['name']) && isset($_GET['gender'])) 
{
   $name = $_GET['name'];
   $gender = $_GET['gender'];
} 
else 
{
   $name = "";
   $gender = "";
}

define("PERSONS_PER_PAGE", 10);


if (isset($_GET['offset'])) 
{
    $offset = $_GET['offset'];
} 
else 
{
    $offset = 0;
}


list($person_list, $num_persons) = get_persons($name, $gender, $offset, PERSONS_PER_PAGE);

$ages = array();


for ($i = 0; $i < count($person_list); $i++)
{
    $ages[$i] = age($person_list[$i].$dateOfBirth, time());
}

$smarty = new Smarty;
$smarty->assign("name", $name);
$smarty->assign("gender", $gender);
$smarty->assign("persons_per_page", PERSONS_PER_PAGE);
$smarty->assign("interests", $interests);
$smarty->assign("offset", $offset);
$smarty->assign("num_persons", $num_persons);
$smarty->assign("person_list", $person_list);
$smarty->assign("ages", $ages);
$smarty->display("index.tpl");

不要太担心$ offset和PERSONS_PER_PAGE,这只用于分页。

我的年龄函数如下:

function age($birth)
{
   $birth_date = new DateTime();
      $birth_date = $birth;
   $birth_date->setTimestamp($birth);

   $now = time();

   $now_date = new DateTime();
   $now_date->setTimestamp($now);

   $interval = $birth_date->diff($now_date); // $interval is a DateInterval


   $age = $interval->y; // number of years in the interval
   return $age;
}

另外,如果它是相关的;我的get_persons函数如下所示:

function get_persons($name, $gender, $offset, $persons_per_page) 
{
    $connection = mysql_open();

    $query = "SELECT SQL_CALC_FOUND_ROWS id, username, gender, dateOfBirth FROM Person";
    if ($name && $gender) 
    {
        $query .= " WHERE name like '%$name%' AND gender like '%$gender%'";
    }
    $query .= " order by id";
    $query .= " LIMIT $offset, $persons_per_page";
    // print "$query<br>\n";

    $result = mysql_query($query, $connection) or show_error();

    $r = mysql_query("SELECT FOUND_ROWS()", $connection) or showerror();
    $r = mysql_fetch_array($r);
    $num_entries = $r[0];

    $entries = array();

    $person_list = array();
    while ($person = mysql_fetch_array($result)) {
        $person_list[] = $person;
    }

    mysql_close($connection) or show_error();
    return array($person_list, $num_entries);
}

,一个人的表格如下:

create table if not exists Person
(
  id int not null auto_increment primary key,
  username varchar(10) not null,
  name varchar(40) not null,
  gender varchar(1) not null,
  dateOfBirth timestamp not null,
  email varchar(40) not null
);

这会产生以下错误:

Notice: Undefined variable: dateOfBirth in /net/homes.ict.griffith.edu.au/export/home/s2737451/public_html/wp/labs/lab5/index.php on line 39 Fatal error: Call to a member function setTimestamp() on a non-object in /net/homes.ict.griffith.edu.au/export/home/s2737451/public_html/wp/labs/lab5/includes/defs.php on line 167

在此先感谢,我真的需要帮助:(

3 个答案:

答案 0 :(得分:1)

<强>更新

  

我试过了,但它仍然给了我   错误:Fatal error: Call to a member function setTimestamp() on a non-object in /net/homes.ict.griffith.edu.au/export/home/s2737451/public_html/wp/labs/lab5/inc­ludes/defs.php on line 167

我认为这是因为你覆盖了你的DateTime对象:

$birth_date = new DateTime();
$birth_date = $birth; // you overwrite the instance of DateTime you created with another value
$birth_date->setTimestamp($birth);

应该是:

$birth_date = new DateTime();
$birth_date->setTimestamp($birth);

如错误所示,您尝试在未定义变量时使用变量$dateOfBirth

$ages[$i] = age($person_list[$i].$dateOfBirth, time());

我认为问题是你混淆了你的smarty和php语法......那条线应该是:

$ages[$i] = age($person_list[$i]['dateOfBirth'], time());

另一方面,为什么你为年龄使用单独的数组呢?为什么不把这个元素添加到每个人的值数组中呢?

foreach($person_list as $i => $person)
{
    $person_list[$i]['age'] = age($person['dateOfBirth'], time());
}

答案 1 :(得分:1)

我认为prodigitals解决了你的问题,但对你的代码有一些错误的建议:

  • 您的SQL查询可能会受SQL Injection次攻击。这是因为您直接将用户输入(GET参数)插入到SQL查询字符串中 - 一个很大的 NO NO 。对于字符串值,您应该使用字符串转义函数,以确保将字符串安全地插入到查询中。对于MySQL,请使用mysql_escape_string()函数:

    $query .= " WHERE name like '%" . mysql_escape_string($name) . "%' AND gender like '%" . mysql_escape_string($gender) . "%'";
    

    对于数字值,您应该检查以确保用户输入了一个数字而不是其他字母。您可能希望$offset始终为正数,因此您可以使用ctype_digit()函数确保变量仅包含数字字符(0-9)。 $persons_per_page的值在您的脚本中是硬编码的,因此您不必担心它。

    if (!ctype_digit($offset)){
       //set to a default value if it's not a positive integer
       $offset = 10;
    } 
    $query .= " LIMIT $offset, $persons_per_page";
    
  • 您可以简化计算返回的人员条目数的代码。使用mysql_num_rows()函数:

    $num_entries = mysql_num_rows($result);
    

答案 2 :(得分:0)

或者您可以通过更改sql命令来执行此操作:

function get_persons($name, $gender, $offset, $persons_per_page) 
{
    $connection = mysql_open();

   $query = "SELECT SQL_CALC_FOUND_ROWS id, username, gender, (YEAR(NOW())-YEAR(dateOfBirth)) as age FROM Person";
    if ($name && $gender) 
    {
        $query .= " WHERE name like '%$name%' AND gender like '%$gender%'";
    }
    $query .= " order by id";
    $query .= " LIMIT $offset, $persons_per_page";

    $result = mysql_query($query, $connection) or show_error();

    $r = mysql_query("SELECT FOUND_ROWS()", $connection) or showerror();
    $r = mysql_fetch_array($r);
    $num_entries = $r[0];

    $entries = array();

    $person_list = array();
    while ($person = mysql_fetch_array($result)) {
        $person_list[] = $person;
    }

    mysql_close($connection) or show_error();
    return array($person_list, $num_entries);
}