0

ive got few client list with DOB format yyyy-mm-dd.
how do i search client by their Date of birth, even if i enter either year or month or date first, it will show the result.

at the moment, i need to use format yyyy-mm-dd to search them.

$result = $db->query("SELECT f_name, l_name, dob, email FROM user WHERE dob LIKE'$keyword%' OR f_name LIKE '$keyword%' OR l_name LIKE '$keyword%' LIMIT 100")

if($result) {
  if($db->affected_rows()!= 0){

     // echo the result

  }
}
4
  • 2
    Not entirely sure what you're asking here. Can you give us a few examples for what you'll be searching for? Commented Dec 13, 2011 at 12:32
  • 2
    A side-note: usually, affected_rows returns the rows affected by an INSERT, DELETE, REPLACE or UPDATE statement. Not from SELECT, though. Commented Dec 13, 2011 at 12:38
  • Are these dates stored as YYYY-MM-DD VARCHAR/CHAR fields, or as DATE fields? Also, am I right in thinking that what you're asking is to input the date as a keyword in any arbitrary format, like "DD/MM/YYYY" or "MM/DD/YYYY"? Commented Dec 13, 2011 at 12:43
  • @Wooble its date field. thats correct. any idea how to search the DOB even if the format is different? Commented Dec 13, 2011 at 12:45

2 Answers 2

2

Rather than LIKE, you can use YEAR(), MONTH(), DAY() to compare against. Be sure you've properly escaped $keyword already...

$result = $db->query("
  SELECT f_name, l_name, dob, email
  FROM user 
  WHERE
    // The year, month, or day can be an exact match (note parentheses)
    (YEAR(dob) = '$keyword' OR MONTH(dob) = '$keyword' OR DAY(dob) = '$keyword')
    // Or the full yyyy-mm-dd date can be an exact match
    OR DATE(dob) = '$keyword'
    // OR UK date format
    OR DATE(dob) = STR_TO_DATE('$keyword', '%d/%m/%y')
    // or the rest of your conditions...
    OR f_name LIKE '$keyword%' OR l_name LIKE '$keyword%' LIMIT 100"
);

EDIT: added quotes around integer date components, to avoid syntax errors for when strings rather than numbers are searched.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks youre really helpful :). i forgot somthing to ask in the question. is it possible to search DOB with UK format (dd/mm/yyyy) even though the date format in database is (yyyy-mm-dd)? how do i do that :)
@boyee007 Maybe via STR_TO_DATE(). See addition above. I'm not sure if the function call will fail if the input doesn't match the format dd/mm/yy.
1

In my opinion, it would be better to normalize date in PHP:

$date = strtotime($keyword);
$result = $db->query('
  SELECT f_name, l_name, dob, email
  FROM user 
  WHERE
    f_name LIKE "$keyword%"
    OR l_name LIKE "$keyword%"
    '.($date&&$date!=-1?'OR dob="'.date('Y-m-d',$date).'"':'').'
  LIMIT 100
');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.