is PDO execute function is limited to accept arguments?
I have a working pdo integrated database class. All of my queries are working very efficiently but when I try to pass a ce开发者_高级运维rtain value, I got stuck. This value is:
DATE_SUB('2010-11-03', INTERVAL 30 DAY)
Now after all my working of class query is prepared like:
SELECT DATE(added) AS date FROM data WHERE DATE(added) <= ?
When i m trying to pass the value: DATE_SUB('2010-11-03', INETRVAL 30 DAY)
to execute()
function,no result is received(but query is executing successfully).
i tried sending raw value '2010-11-03'
. Its working correctly but not wid this function.
Do anybody know y this mysql function is not being passed to execute function?
You cannot use placeholders as a replace this portion of my SQL query : placeholders and their corresponding bound values, can only correspond to values.
When you're passing 2010-11-03
, you're passing a value -- so it works.
When you're trying to pass DATE_SUB('2010-11-03', INTERVAL 30 DAY)
, it's not a value -- so it doesn't work.
Here, you'll have to rewrite your Prepared statement, so the placeholder corresponds to the date, and not a whole expression :
SELECT DATE(added) AS date
FROM data
WHERE DATE(added) <= DATE_SUB(?, INTERVAL 30 DAY)
(The 30
could also be replaced with a placeholder, if needed)
SELECT DATE(added) AS date
FROM data
WHERE DATE(added) <= DATE_SUB(?, INTERVAL ? DAY)
and treat both the date and interval as bind values
精彩评论