Using AND/OR mysql commands with FROM_UNIXTIME
Trying to select a query in php/mysql to get "Upcoming Items" in a calendar. We store the 开发者_StackOverflowdates in the DB as a unix time. Here's what my query looks like right now
SELECT *
FROM `calendar`
WHERE (`eventDate` > '$yesterday')
OR (FROM_UNIXTIME(eventDate, '%m') > '$current_month' AND `$yearly` = '1')
ORDER BY `eventDate`
LIMIT 4
This is giving me an error "Unknown column '' in 'where clause'". I'm sure it has to do with my use of parenthesis (which I've never used before in a query) and the FROM_UNIXTIME command.
Can someone help me out and let me know how I've screwed this up?
Thanks!
This to me looks suspicious:
`$yearly`
What is the value of $yearly? Is it empty? When you use backticks MySQL treats the contents as the name of a column.
Perhaps you meant to create a string instead, in which case you should use a different type of quote:
'$yearly' = '1'
Or perhaps you intended to refer to the column yearly
:
yearly = '1'
Another tip is to print the SQL query after the PHP variables have been interpolated as this sometimes makes it easier to understand the error message from MySQL. I'd imagine your query looks something like this:
SELECT *
FROM `calendar`
WHERE (`eventDate` > '1295071200')
OR (FROM_UNIXTIME(eventDate, '%m') > '1' AND `` = '1')
ORDER BY `eventDate`
LIMIT 4
The suspicious part is here:
`` = '1'
Do you have a column named $yearly ?, try removing the dollar sign
精彩评论