在mysql、php中搜索年龄范围

发布于 2024-10-31 04:11:45 字数 1070 浏览 3 评论 0原文

嘿伙计们,我需要制作一个函数,以便它可以在这个函数中搜索年龄范围,

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我要还你自由 2024-11-07 04:11:45

如果数据库中有 DOB,并且想要查找与年龄范围匹配的行,最简单、最有效的方法就是首先将您的最小年龄和最大年龄转换为 php 中的 DOB。那么您的查询将类似于:

SELECT * FROM USERS WHERE DOB > MIN_DOB AND DOB <= MAX_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:

SELECT * FROM USERS WHERE DOB > MIN_DOB AND DOB <= MAX_DOB;

assuming DOB is a timestamp or date or long.

似梦非梦 2024-11-07 04:11:45

您需要获取 $query 的开始年龄和结束年龄部分,然后将开始年龄和结束年龄转换为出生日期。

然后,您可以添加

"OR dob BETWEEN '%$startdob%' AND '%$enddob%'"

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

"OR dob BETWEEN '%$startdob%' AND '%$enddob%'"

which should evaluate to something like OR dob BETWEEN '1980-01-01' AND '1990-01-01'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文