在mysql、php中搜索年龄范围
嘿伙计们,我需要制作一个函数,以便它可以在这个函数中搜索年龄范围,
function search($query) {
$query = mysql_escape_string($query);
$connection = mysql_open();
$statement = "select ID, UserName, Gender, USERDOB,DATEDIFF(USERDOB,now()) from Users ";
if (!empty($query)) {
$statement .= "where FirstName like '%$query%' " .
"or LastName like '%$query%' " .
"or UserName like '%$query%' " .
"or Gender like '%$query%' ";
}
$statement .= "order by id ";
$result = @ mysql_query($statement, $connection)
or showerror();
$users = array();
while ($user = mysql_fetch_array($result)) {
$user[4] = floor($user[4]/-1/364.25);
$users[] = $user;
}
// Close connection and return resulting array
mysql_close($connection)
or showerror();
return $users;
}
这就是搜索函数。但它也可以查看一个人的年龄。年龄不存储在数据库中,但出生日期存储在数据库中。所以它是这样计算的。我也在使用 smarty 模板。 mysql_open() 是我自己的函数。我想我需要找到一种方法将年龄纳入查询并在范围内做一些事情......
无论如何都迷失了,有什么想法吗?
Hey guys, I need to make a function so that it will search an age range in this function
function search($query) {
$query = mysql_escape_string($query);
$connection = mysql_open();
$statement = "select ID, UserName, Gender, USERDOB,DATEDIFF(USERDOB,now()) from Users ";
if (!empty($query)) {
$statement .= "where FirstName like '%$query%' " .
"or LastName like '%$query%' " .
"or UserName like '%$query%' " .
"or Gender like '%$query%' ";
}
$statement .= "order by id ";
$result = @ mysql_query($statement, $connection)
or showerror();
$users = array();
while ($user = mysql_fetch_array($result)) {
$user[4] = floor($user[4]/-1/364.25);
$users[] = $user;
}
// Close connection and return resulting array
mysql_close($connection)
or showerror();
return $users;
}
This is the function for search. But it also makes it possible to view a persons age. Age is not stored in the database but DOB is. So it calculates that. I also am using a smarty template.
mysql_open() is my own function. I figure I need to find a way to get age into the query and do some thing with range...
anyway quite lost, any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果数据库中有 DOB,并且想要查找与年龄范围匹配的行,最简单、最有效的方法就是首先将您的最小年龄和最大年龄转换为 php 中的 DOB。那么您的查询将类似于:
假设 DOB 是时间戳、日期或长整型。
If you have DOB in the database and you want to find rows that match an age range the simplest and most efficient thing to do is convert your min age and your max age into DOBs in your php first. Then your query would be something like:
assuming DOB is a timestamp or date or long.
您需要获取 $query 的开始年龄和结束年龄部分,然后将开始年龄和结束年龄转换为出生日期。
然后,您可以添加
which 应该计算为 OR dob BETWEEN '1980-01-01' AND '1990-01-01' 之类的内容
You need get the start and end age parts of $query, and then convert start age and end age to dates of birth.
You can then add
which should evaluate to something like OR dob BETWEEN '1980-01-01' AND '1990-01-01'