简体   繁体   中英

PHP MySQL Check If String Contains Query In Only First Few Characters

I'm wondering if there is a way to check the first few characters of a string without having to select the string from the database first.

I am making an advanced search feature to add on to my websites search feature. The advanced search feature allows you to search by date. The date in the database looks like 2/1/2012 . If the user enters February , I convert it to the number 2 . I then query the database using this line (not the exact line):

mysql_query(SELECT * FROM database WHERE date LIKE '%2%');

However, this returns all rows with the number 2 in the date. I would like to search only the first two characters for the number. Is this possible using a MySQL query, or do I have to do it the longer way?

Please use the Mysql Date Data Type for your field.

Then you can search by ranges of dates and all other nice features you would like to add, as well as converting the date data to present differently like:

2/2/2012

Feburary 2nd

Feb 2 '12

etc...

Check out the MySQL date reference for more functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM