我在论坛上搜索了相应的代码,或者是否有任何解决方案但尚未解决。我在我的数据库中有这个varchar 12-03-1994(d-m-Y),我的代码选择了该年龄段的用户数。我需要将varchar中的date_of_birth转换为date_format并且我使用str_to_date所以我这样做但是kip得到一个空的结果。我做错了什么?
$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;
答案 0 :(得分:0)
好的,我没有任何测试的东西,但它看起来有效......
// 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);