我已经在论坛上搜索了适当的代码,或者是否有解决方案,但还没有。我的数据库中有这个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;
最佳答案
好的,我对此没有任何测试,但看起来确实可行...
// 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);
关于php - 将字符串转换为日期并在MySQL中使用别名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19989113/