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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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

 

 

 

 

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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