I have the following in database field
id = id,
Xterminal = 1234,
Testdata = 1234',
date BETWEEN $Date1 AND $Date2,
MyVar = $myVar;
I want to use it in my sql statement. I have $Date1 = ‘2011-01-09’ and $Date2 = ‘2011-03-09’ set in my scripte above the sql statement.
I have $myvar setup to $myVar = 3. But it doesnt convert the variables in sql and when I do print_r on $sql it shows the variables and even in query it shows variables not the values of them
I doing the following
$conditions = explode(',',$results['conditions']);
print_r($condidtions) gives
Array
(
[0] =>
id = id
[1] =>
Xterminal = 1234
[2] =>
Testdata = 1234
[3] =>
date BETWEEN $Date1 AND $Date2
[3] =>
MyVar = $myVar;
)
$sql1 = "Select * from table where ";
$sql2 = implode(' AND ',$conditions);
$sql = $Sql1.' '.$sql2;
print_r(“$sql”) gives
SELECT
*
FROM
table
WHERE
id = id AND
Xterminal = 1234 AND
Testdata = 1234 AND
date BETWEEN $Date1 AND $Date2 AND
MyVar = $myVar;
Not sure why is it not taking my variable values that I have defined alreadt at the to of script
It sounds like what you’re saying is that the string
date BETWEEN $Date1 AND $Date2,came from the database. If that is the case, then your variables are not being evaluated, because this is just data…You can use eval(), but this is a dangerous path if users could possibly create these conditions.
Edit: I put together a working PHP sample for this, visible at http://gfosco.kodingen.com/conditions.php and here is the source:
This outputs: