在mysql、php中搜索年龄范围 [英] Search age range in mysql, php

查看:56
本文介绍了在mysql、php中搜索年龄范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我需要创建一个函数,以便在这个函数中搜索年龄范围

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;
}

这是搜索功能.但它也可以查看一个人的年龄.年龄不存储在数据库中,但 DOB 存储在数据库中.所以它计算.我也在使用一个聪明的模板.mysql_open() 是我自己的函数.我想我需要找到一种方法将年龄纳入查询并使用范围做一些事情......

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?

推荐答案

如果您的数据库中有 DOB 并且您想找到与某个年龄范围匹配的行,那么最简单、最有效的方法就是转换您的最小年龄和您的年龄范围.首先在您的 php 中将最大年龄设置为 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;

假设 DOB 是时间戳或日期或长.

assuming DOB is a timestamp or date or long.

这篇关于在mysql、php中搜索年龄范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆