I have a
$dummy = 1
$dummy = 2
$dummy = null
and a query and tried display it
$query = "select * from table1 where sy=2000 AND sy_dummy=$dummy";
echo $query;
then when $dummy=null
, I get
ERROR: syntax error at or near "order" LINE 2: AND sy_dummy= order by table1 ^
and i tried $dummy IS NULL
but gives me blank page.
My problem is that how can I put a NULL
value to my $dummy
and gives me something like the result should be .....sy_dummy IS NULL order by table1
not ......sy_dummy= order by table1;
Is this possible? Hope you understand my problem.
I would check for the null in PHP and modify how you're forming your query string in your php logic.
This allows you to prepare the statement variables (safe and secure) rather than simply entering them inline into the query (potentially dangerous and open to SQL Injection)
if(!isset($dummy) || $dummy === null)
{
// Form your $query here using sy_dummy IS NULL
}
else
{
// Form your $query here using sy_dummy =, and bind your variables
}
Also take a look into PDO and Prepare Statements for writing safe, secure database interactions.
Try this code:
$query="select * from table1 where sy=2000 AND sy_dummy ".(($dummy===null)?'is null':'= '.$dummy);