Jump to content

search help


steviez

Recommended Posts

Hi,

 

I am writing a php script to search through my database of users, I need one of the search criteria to be age (eg: between 30 and 70) but the only problem is that their age is not stored in the database only there date of birth.

 

Without rewriting my script or adding extra functions on search (not sure if its even possible) is there any way of doing this search?

 

Thanks

Link to comment
Share on other sites

you can use the following to get the age from the birthdate, birthdate_field will be your birthdate field in the database

 

floor(datediff(now(), birthdate_field) / 365) 

 

Sorry I should have said, the birthdays are stored as: 2-02-1984.. I am starting to think that it may not be do-able :(

Link to comment
Share on other sites

Except that there are not 356 days in every year and that formula will be off by one day for every leap year the person has been alive (matters to those who's age is being checked close to the day of their birthday.)

 

See the age calculation at this link - http://dev.mysql.com/doc/refman/5.0/en/date-calculations.html (basically take the difference in years, subtract one if the birthday has not occurred yet in the current year.)

Link to comment
Share on other sites

then adding it to a row in the users database should there

 

That would result in duplicate/derived data. It would also create a data management problem because a person's age is not fixed (unless they are dead.) You would need to recalculate the age every time you used it in case their birthday date went past.

 

The link I posted above to the mysql manual section shows a query that calculates the age. You can then use the age value in that same query any way you want.

Link to comment
Share on other sites

then adding it to a row in the users database should there

 

That would result in duplicate/derived data. It would also create a data management problem because a person's age is not fixed (unless they are dead.) You would need to recalculate the age every time you used it in case their birthday date went past.

 

The link I posted above to the mysql manual section shows a query that calculates the age. You can then use the age value in that same query any way you want.

 

I have done what is says on that page but it does not return there age, my query is here:

 

"SELECT mm_users.user_id, mm_users.user_username, mm_users.user_photo, mm_users.user_signupdate, mm_users.user_lastlogindate, mm_users.user_dateupdated, mm_users.user_birthday, CURDATE(), (YEAR(CURDATE())-YEAR(mm_users.user_birthday)) - (RIGHT(CURDATE(),5)<RIGHT(mm_users.user_birthday,5)) AS age FROM mm_profiles LEFT JOIN mm_users ON mm_profiles.profile_id = mm_users.user_id WHERE mm_users.user_privacy_search = '1' $bquery"

Link to comment
Share on other sites

it does not return there age

 

Yes, but what DOES it return? And as previously mentioned in the thread, what data type is mm_users.user_birthday?

 

I get no result its blank, I am not sure what you mean by data type... If you mean the database row type then its varchar(20)

Link to comment
Share on other sites

I am UK based

Computers don't really care about that. Formats like dd-mm-yyyy or mm/dd/yyyy are human conventions and have no significance to a computer. Computers care about things like speed and storage requirements.

 

The YYYY-MM-DD format exists for a couple of reasons -

 

1) When the fields (y, m, d) are in that specific left-to-right order, the values can be directly sorted and compared using less-than/greater-than comparisons,

2) By using a consistent, fixed format, the amount of storage has been minimized and the code has been optimized to result in the fastest queries.

 

There are also a couple dozen built in data/time functions that can be used once your dates are in the expected DATE data type.

 

To output a DATE data type in any format you want, simply use the mysql DATE_FORMAT() function in your query when you SELECT the data.

Link to comment
Share on other sites

I am UK based

Computers don't really care about that. Formats like dd-mm-yyyy or mm/dd/yyyy are human conventions and have no significance to a computer. Computers care about things like speed and storage requirements.

 

The YYYY-MM-DD format exists for a couple of reasons -

 

1) When the fields (y, m, d) are in that specific order, the values can be directly sorted and compared using less-than/greater-than comparisons,

2) By using a consistent, fixed format, the amount of storage has been minimized and the code has been optimized to result in the fastest queries.

 

There are also a couple dozen built in data/time functions that can be used once your dates are in the expected DATE data type.

 

To output a DATE data type in any format you want, simply use the mysql DATE_FORMAT() function in your query when you SELECT the data.

 

Thank you so so much for your help :)

Link to comment
Share on other sites

My query is not working for the search criteria.. after my initial query I have extra statments like: AND username = 'me'

 

The following query wont work:

 

" AND (YEAR(CURDATE())-YEAR(user_birthday)) - (RIGHT(CURDATE(),5)<RIGHT(user_birthday,5)) between '%{$_GET['age_from']}%' and '%{$_GET['age_to']}%'"

 

Any help?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.