[SOLVED] MYSQL CASE statement within PHP

Hi I’m trying to use a MySQL “CASE” statement within PHP to develop a conditional query. The query runs fine within from the MySQL command prompt with the “case” statements. And the PHP code works fine when I remove the lines that begin with “case”. However when I run the query as listed below within my PHP code i get the following:

[Fri Apr 20 22:15:24 2007] [error] [client 10.188.50.74] PHP Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /srv/www/htdocs/mysql-learn/acetrack-post.php on line 68, referer: http://webserver1/mysql-learn/acetrack-post.html

< code snip >

$query ="
SELECT
acetitle, tracking_no, aceamt,
case when (To_days(curdate()) - To_days(create_date)) >= 90 Then pototalamt else “” end as “90past” ,
case when (To_days(curdate()) - To_days(create_date)) < 90 and (To_days(curdate()) - To_days(create_date)) > 30 Then pototalamt else “” end
as “30-90past”
FROM Purtable t
LEFT JOIN acelog a
ON ace=acenum
WHERE ace=‘0220’
GROUP by po ASC
";

$result = mysql_query($query);

< /code snip >

Thanks in advance for any help.

You’re using double quotes both to define the query and to define strings inside the query. PHP doesn’t like that. Change either the query delimiters or the string delimiters to single quotes, much like you did on the line WHERE ace=‘0220’.

Sponsor our Newsletter | Privacy Policy | Terms of Service