Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem using custom value in query 1

Status
Not open for further replies.

solepixel

Programmer
May 30, 2007
111
US
It's been a while since I wrote a "complicated" query and I can't figure out why this isn't work:


Code:
$querydate = "2007-07";

$query = "SELECT *,
   DATE_FORMAT(`date`,'%M %e, %Y') AS disp_date,
   DATE_FORMAT(`date`, '%m-%d') AS sDate,
   DATE_FORMAT(`date`, '%Y-%m') AS querydate
  FROM `testimonials`
  WHERE `deleted` <> 1 AND
   querydate = '".$querydate."'
  ORDER BY `winner` DESC,
   `sdate` DESC";

When I try this, I get an unknown column "querydate". Can you not use a variable created within the query in the WHERE part? Am I doing something seriously wrong?
 
as you allude, i don't think you can reference created vars like that.

i would do what you are trying to do as follows
Code:
$year = '2007';
$month = '7';
$query = "SELECT *,
   DATE_FORMAT(`date`,'%M %e, %Y') AS disp_date,
   DATE_FORMAT(`date`, '%m-%d') AS sDate,
   DATE_FORMAT(`date`, '%Y-%m') AS querydate
  FROM `testimonials`
  WHERE `deleted` <> 1 AND
   year(`date`) = '$year' AND
 month(`date`) = '$month'
  ORDER BY `winner` DESC,
   `sdate` DESC";
 
ah, thank you so much jp for reminding me how to do this properly...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top