I have several strings stored in a database (yes, I know it’s not the best practice, but I’m working with a system someone else designed and I cannot change it.)
For example, in my table called specialDates, I have a computedDate column that contains this:
date('d', strtotime("thursday, november ".date("Y")." + 3 weeks"))
This is pulled into my code and stored in a variable called $request['order_date'].
This, when used in standard PHP code, returns the date that Thanksgiving falls on in the current year. I need to pass the evaluated value of this (for this year – 2013 – the value is 28) to another query. But I can’t seem to get the value to evaluate.
$query = 'SELECT count(orderID) as numberOfOrders FROM customer_orders WHERE customerID = ? AND orderDate = ?';
$param = array($request['customer_id'], eval($request['order_date']));
doesn’t work. And when I just try to echo out the value to the screen, doing this:
echo eval($request['order_date']);
or this:
echo eval(" return \$request['order_date']; ");
just prints out the literal value of the string (i.e. date('d' etc.) rather than the expected evaluated value.
What am I doing wrong?
UPDATED
I tried AScherer’s answer below, and although it does work when I just echo the value to the screen:
echo eval(" return {$request['order_date']}; ");
it does not work when I try to use the eval inside my array:
$param = array($request['customer_id'], eval("return {$request['order_date']}"));
How can I get this working so that the value I’m passing in the array is the actual value of 28, rather than the string pre-eval?
You were escaping the $request in your return part, which you dont want to do. This method should work, but id suggest adding return the the row in the database so u dont have to modify the eval