Jump to content

[SOLVED] query by year only


eagleweb

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/71951-solved-query-by-year-only/
Share on other sites

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.

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

<?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?

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;

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

 

 

 

 

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;

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.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.