5kyy8lu3 Posted February 9, 2009 Share Posted February 9, 2009 I have PHP 5.0.45. Hi. I'm fairly new to MySQL. I'm self taught from the php/mysql for dummies book. Though it covered php pretty well, I felt it lacked in the mysql department a bit. I've never used LIKE or the wildcard in a mysql query before. I searched and I believe I got this right but I want to make sure I did this query correctly. I don't have any dates in my table from last year so it's hard for me to test this. What I want: to select entries in the table where the timestamp is from this year. I use YYYYMMDDHHMMSS for my timestamp format. Is my query correct for pulling ALL entries from the table that are from this year? <?php // my php line: $q = 'SELECT miles FROM ' . $username . ' WHERE dt_added LIKE ' . date("Y") . '%'; //which should result in a query like this: SELECT miles FROM admin WHERE dt_added LIKE 2008% would this query accomplish what I'm wanting it to? I've read online and bought the o'reilly's mysql book today and I believe this should work but when I add php into the mix I'd just like to be 100% sure. Thanks! Link to comment https://forums.phpfreaks.com/topic/144457-solved-select-like/ Share on other sites More sharing options...
Mchl Posted February 9, 2009 Share Posted February 9, 2009 $q = 'SELECT miles FROM ' . $username . ' WHERE YEAR(dt_added) = YEAR(CURDATE())'; Link to comment https://forums.phpfreaks.com/topic/144457-solved-select-like/#findComment-758008 Share on other sites More sharing options...
5kyy8lu3 Posted February 9, 2009 Author Share Posted February 9, 2009 $q = 'SELECT miles FROM ' . $username . ' WHERE YEAR(dt_added) = YEAR(CURDATE())'; lol man I was way off! so out of curiousity, would my method even work? i knew mysql had some built in functions for handling dates and times but I didn't know I could use it with my weird timestamp format appreciate the help and fast responce, cheers Link to comment https://forums.phpfreaks.com/topic/144457-solved-select-like/#findComment-758010 Share on other sites More sharing options...
Mchl Posted February 9, 2009 Share Posted February 9, 2009 You're not storing this timestamp as a string, do you? Date functions will work on MySQLs DATE, TIME, DATETIME and TIMESTAMP fields. If any book tells you to store date, timestamp or anything like that as a string - burn it, as it is source of evil. Link to comment https://forums.phpfreaks.com/topic/144457-solved-select-like/#findComment-758015 Share on other sites More sharing options...
5kyy8lu3 Posted February 9, 2009 Author Share Posted February 9, 2009 You're not storing this timestamp as a string, do you? Date functions will work on MySQLs DATE, TIME, DATETIME and TIMESTAMP fields. If any book tells you to store date, timestamp or anything like that as a string - burn it, as it is source of evil. just bought the book today so I haven't had time to read it front to back yet, but yes, I've been storing it as VARCHAR, oops =) but I'm guessing I should ALTER TABLE and change it to DATETIME, yea? I wrote functions to format my timestamps. I'm guessing I would've saved myself a TON of time had I just used DATETIME format and the built in date/time mysql functions lol Link to comment https://forums.phpfreaks.com/topic/144457-solved-select-like/#findComment-758020 Share on other sites More sharing options...
Mchl Posted February 9, 2009 Share Posted February 9, 2009 Definitively store your time values using dedicated column types. Saves a lot of hassle and (MySQL's) memory. Link to comment https://forums.phpfreaks.com/topic/144457-solved-select-like/#findComment-758021 Share on other sites More sharing options...
5kyy8lu3 Posted February 9, 2009 Author Share Posted February 9, 2009 Definitively store your time values using dedicated column types. Saves a lot of hassle and (MySQL's) memory. Will do! Thanks for the help Link to comment https://forums.phpfreaks.com/topic/144457-solved-select-like/#findComment-758022 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.