osyrys14 Posted March 29, 2013 Share Posted March 29, 2013 I'm trying to make a search where I pull information out of the column that is date/time (2013-03-29 14:00:00) and have a form search the information by a range provided. I was helped in another thread and I was incorrect in the procedure I need to do, but this is the code I'm using to select * from the db, but what I need to do is display just the ones between the date chosen on the form that submits the query. Any and all help is greatly appreciated! $mydate1 = sprintf('%4d-%02d-%02d', intval($_GET['year']), intval($_GET['month']), intval($_GET['day'])); $mydate2 = sprintf('%4d-%02d-%02d', intval($_GET['year2']), intval($_GET['month2']), intval($_GET['day2'])); $sql = "SELECT * FROM $db_tb_name WHERE `$db_tb_atr_name` LIKE '%$query%' AND `6` > '$mydate1' AND `6` < '$mydate2'"; Quote Link to comment https://forums.phpfreaks.com/topic/276280-searchsplitcompare/ Share on other sites More sharing options...
Psycho Posted March 29, 2013 Share Posted March 29, 2013 You have a field named '6'? Interesting. $sql = "SELECT * FROM $db_tb_name WHERE `$db_tb_atr_name` LIKE '%$query%' AND `6` BETWEEN '$mydate1' AND '$mydate2'"; Quote Link to comment https://forums.phpfreaks.com/topic/276280-searchsplitcompare/#findComment-1421746 Share on other sites More sharing options...
osyrys14 Posted March 29, 2013 Author Share Posted March 29, 2013 It's a dummy database to get the information functional and I have not gotten header names from the client yet so right now, they are all 1, 2, 3, 4, 5, 6, 7, 8, etc. That gets me the correct query string, but since the query is set to just a date format, and the table is date/time, I'm still getting no results. Is there a way to split at the space between the date and time and search that way? The format in the db is 2013-03-29 14:00:00 and the form/search is only 2013-03-29. Thanks so much for the help! Quote Link to comment https://forums.phpfreaks.com/topic/276280-searchsplitcompare/#findComment-1421747 Share on other sites More sharing options...
Solution Psycho Posted March 29, 2013 Solution Share Posted March 29, 2013 Then convert them to timestamps. MySQL has a ton of date/time functions. Just go read up on them to see what one meets your needs. http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timestamp $sql = "SELECT * FROM $db_tb_name WHERE `$db_tb_atr_name` LIKE '%$query%' AND `6` BETWEEN TIMESTAMP('$mydate1') AND TIMESTAMP('$mydate2')"; Quote Link to comment https://forums.phpfreaks.com/topic/276280-searchsplitcompare/#findComment-1421750 Share on other sites More sharing options...
osyrys14 Posted March 29, 2013 Author Share Posted March 29, 2013 I get this as what it's looking for... SELECT * FROM cdr WHERE `8` LIKE '%%' AND `6` BETWEEN TIMESTAMP('2012-09-02') AND TIMESTAMP('2012-09-03')and no search results Quote Link to comment https://forums.phpfreaks.com/topic/276280-searchsplitcompare/#findComment-1421751 Share on other sites More sharing options...
Psycho Posted March 29, 2013 Share Posted March 29, 2013 Probably because of this WHERE `8` LIKE '%%' I tested the process of using TIMESTAMP() for a string formatted as a date against a field that was a datetime - so I know it works. Quote Link to comment https://forums.phpfreaks.com/topic/276280-searchsplitcompare/#findComment-1421752 Share on other sites More sharing options...
osyrys14 Posted March 29, 2013 Author Share Posted March 29, 2013 I put something in the text box on the search, now it gets this SELECT * FROM cdr WHERE `8` LIKE '%EFM%' AND `6` BETWEEN TIMESTAMP('2012-09-02') AND TIMESTAMP('2012-09-03')It's searching 2 fields, first one ( is an ID code, the other we've been working on is a time/date stamp. Is it possible to make them work together? Quote Link to comment https://forums.phpfreaks.com/topic/276280-searchsplitcompare/#findComment-1421753 Share on other sites More sharing options...
osyrys14 Posted March 29, 2013 Author Share Posted March 29, 2013 Nevermind, it's working. Quote Link to comment https://forums.phpfreaks.com/topic/276280-searchsplitcompare/#findComment-1421754 Share on other sites More sharing options...
osyrys14 Posted March 29, 2013 Author Share Posted March 29, 2013 Thank you so much for your help! Quote Link to comment https://forums.phpfreaks.com/topic/276280-searchsplitcompare/#findComment-1421755 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.