dongyao2001 2013-11-14 21:49
浏览 102

将字符串转换为日期并在mysql中使用别名

I have searched the forum for the appropriate code or if there is any solution but none yet. I have in my database this varchar 12-03-1994 (d-m-Y) and my code selects the number of users in that age range. I needed to convert the date_of_birth which is in varchar to date_format and I used str_to_date so I did this but kip getting an empty result. what am I doing wrong?

$aged = $conn->prepare("SELECT COUNT(STR_TO_DATE('date_of_birth','%d-%m-%Y')) as year FROM tbl_members WHERE date_of_birth BETWEEN CURDATE() - INTERVAL 105 YEAR AND 
    CURDATE() - INTERVAL 36 YEAR AND resultselect = ?");
$aged->execute(array($pid));
$recordaged = $aged->fetchObject();
$recorda = $recordaged->year;
  • 写回答

1条回答 默认 最新

  • dongma7796 2013-11-15 17:44
    关注

    Ok, I don't have anything for testing this on, but it looks like it works...

    // needs variables $conn and $pid
    $aged = $conn->prepare("SELECT STR_TO_DATE('date_of_birth','%d-%m-%Y') as converted_date FROM tbl_members WHERE converted_date BETWEEN CURDATE() - INTERVAL 105 YEAR AND CURDATE() - INTERVAL 36 YEAR AND resultselect = ?");
    $result = $aged->execute(array($pid));
    $count = mysql_num_rows($result);
    
    评论

报告相同问题?