eagleweb Posted October 5, 2007 Share Posted October 5, 2007 I am trying to only pull rows from the db where the 'tour_date' year = the present year. Here is my query which does not work: $queryres = mysql_query("SELECT * FROM reservations WHERE ".date('Y', tour_date)." = '".date('Y')."' ORDER by tour_date DESC", $mysql_connect); Can someone tell me why this simple code is not pulling any results? By the way, tour_date is inserted from a form and was set up like this (if it matters) $day = $_POST['d']; $month = $_POST['m']; $year = $_POST['y']; $timeStamp = mktime(0,0,0, $month, $day, $year); Thanks in advance for any help. Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/ Share on other sites More sharing options...
jd2007 Posted October 5, 2007 Share Posted October 5, 2007 ok, if the tour_date type is mysql's date/datetime type, do this.... $year=gmstrftime("%Y"); $year.="-"; $queryres = mysql_query("SELECT * FROM reservations WHERE tour_date='%$year%' ORDER by tour_date DESC", $mysql_connect); its better for you to do your querying this way : $query= "SELECT * FROM reservations WHERE tour_date='%$year%' ORDER by tour_date DESC"; $res=mysql_query($query, $mysql_connect); try and see if it works...then, tell us. Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362418 Share on other sites More sharing options...
eagleweb Posted October 5, 2007 Author Share Posted October 5, 2007 It does not work. I get a '0' for mysql_num_rows($queryres) I see that you are taking the mysql year string and seeing if it exists in the tour_date string. What does the $year.="-"; do? Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362469 Share on other sites More sharing options...
jd2007 Posted October 5, 2007 Share Posted October 5, 2007 remove $year.="-";....what happens ? Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362472 Share on other sites More sharing options...
eagleweb Posted October 5, 2007 Author Share Posted October 5, 2007 Same thing. I did and echo on the $year and get 2007-, therefore all the$year.="-"; does is add a dash after the year. tour_date is a timestamp. It looks like this in the db: 1199336400 $year will not find a match. By the way, why do you prefer to place the db connect 'mysql_connect' on a seperate line? I have always done it the way you prefer, but recently changed to the other way. Just curious why it is better. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362481 Share on other sites More sharing options...
jaimitoc30 Posted October 5, 2007 Share Posted October 5, 2007 Hi, do you have an example of a registry of the tour_date field so I can see the format it has and then I can provide you with a query that may help you. Regards Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362485 Share on other sites More sharing options...
MmmVomit Posted October 5, 2007 Share Posted October 5, 2007 $queryres = mysql_query("SELECT * FROM reservations WHERE YEAR(tour_date) = $var_containing_year ORDER by tour_date DESC", $mysql_connect); Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362501 Share on other sites More sharing options...
eagleweb Posted October 5, 2007 Author Share Posted October 5, 2007 <?php $year = date('Y'); $queryres = mysql_query("SELECT * FROM reservations WHERE YEAR(tour_date) = '".$year."' ORDER by tour_date DESC", $mysql_connect); $rowres = mysql_fetch_assoc($queryres); $totalRowsres = mysql_num_rows($queryres); if($totalRowsres > 0) { echo $totalRowsres; } ?> I get 0 results when I know I have approx 7 with this year in it. I had no clue that trying to match the tour_date to the present year was going to give me a difficult time. Maybe if I would have used Y-m-d for the tour_date instead... Any more ideas? Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362578 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 i'd back up and take a look at the data we're comparing: SELECT YEAR(tour_date) FROM reservations do you see proper years here? if so, then echo the $sql so we can see that too. oh, it's wrapped up in mysql_query. pull it out into it's own variable and echo: $sql = "your sql statement"; $queryres = mysql_query($sql) or die(mysql_error()); echo $sql; Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362581 Share on other sites More sharing options...
MmmVomit Posted October 5, 2007 Share Posted October 5, 2007 Try getting rid of the single quotes around $year Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362584 Share on other sites More sharing options...
eagleweb Posted October 5, 2007 Author Share Posted October 5, 2007 I removed the single quotes but still the same. It makes no sense. Since the tour_date is a timestamp, doesn't it need to be told to strtotime or something before it will recognize that there is a year in there? something like WHERE YEAR(strtotime(0, tour_date("Y"))) or YEAR(date('Y', tour_date))? I did what BlueSkyIS wrote: SELECT * FROM reservations WHERE YEAR(tour_date) = 2007 ORDER by tour_date DESC Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362597 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 Try getting rid of the single quotes around $year this will have no effect. there is no reason to NOT use single quotes around every SQL value. imo, it's best to consistently ALWAYS use quotes than to worry about what does or doesn't get quoted. Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362605 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 i'd back up and take a look at the data we're comparing: SELECT YEAR(tour_date) FROM reservations do you see proper years here? if so, then echo the $sql so we can see that too. oh, it's wrapped up in mysql_query. pull it out into it's own variable and echo: $sql = "your sql statement"; $queryres = mysql_query($sql) or die(mysql_error()); echo $sql; Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362607 Share on other sites More sharing options...
eagleweb Posted October 5, 2007 Author Share Posted October 5, 2007 BlueSkyIS I did that. This was the echo $sql SELECT * FROM reservations WHERE YEAR(tour_date) = 2007 ORDER by tour_date DESC Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362617 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 i'd back up and take a look at the data we're comparing: SELECT YEAR(tour_date) FROM reservations do you see proper years here? Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362619 Share on other sites More sharing options...
eagleweb Posted October 5, 2007 Author Share Posted October 5, 2007 Not in the SELECT YEAR(tour_date) but the = '".$year."' does show 2007 So, how do we pull the year out of the timestamp tour_date so that our query works? I have tried multiple things such as WHERE ".date('Y', tour_date)." = '".$year."' but it does not seem to work. Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362720 Share on other sites More sharing options...
MmmVomit Posted October 5, 2007 Share Posted October 5, 2007 i'd back up and take a look at the data we're comparing: SELECT YEAR(tour_date) FROM reservations do you see proper years here? ^^^ run this query and post the output ^^^ Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362722 Share on other sites More sharing options...
eagleweb Posted October 5, 2007 Author Share Posted October 5, 2007 <?php $query= "SELECT YEAR(tour_date) FROM reservations"; $res=mysql_query($query, $mysql_connect); echo "RESULTS:<br>"; do { echo $res; echo "<br>"; } while ($res = mysql_fetch_assoc($query)); ?> RESULTS: Resource id #7 Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362735 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 we'd like to see the data output, not the resource identifier. you were close, but what we want is in here: while ($res = mysql_fetch_assoc($query)) { print_r($res); // We want to see the data returned.... } eh, rough but it'll do.. Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362739 Share on other sites More sharing options...
Barand Posted October 5, 2007 Share Posted October 5, 2007 If you are storing as 1199336400 then it needs converting to datetime format first ... WHERE YEAR(FROM_UNIXTIME(mydatecolumn)) = '2007' Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362770 Share on other sites More sharing options...
eagleweb Posted October 5, 2007 Author Share Posted October 5, 2007 Barand, You hit it on the head!!! Were you just sitting back watching and laughing at me; waiting for the right time to pop in? LOL Thanks alot. Thanks to BlueSkyIS and MmmVomit also for your time. AWESOME! Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-362793 Share on other sites More sharing options...
Barand Posted October 6, 2007 Share Posted October 6, 2007 I saw this topc with 18 replies for what looked like a simple problem. Just thought I'd jump in before the thread filled the server. Quote Link to comment https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/#findComment-363023 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.