PDO check field against prepared: param, processing IS NULL conditions

I have the following query:

//query: $query = " SELECT field1 FROM table WHERE field2 = :PARAM1 AND ... AND fieldx = :PARAM1X"; //params: $params = array(); $params [":PARAM" . $i] = NULL; //prepare and execute: $o = $cnx->prepare($query); $o->execute($params); 

How can I bind parameters with NULL values? Will PDO automatically change = :PARAM1 to IS NULL ? To be clear, trying to compute WHERE field = null does not work in mysql and will never return anything. Instead, we should use WHERE field IS NULL .

What I'm dealing with right now.

I have to say that my first tests are positive, but I really do not want to detect a side effect after 6 months in the production environment ...

+6
source share
2 answers

A comment from PHP Docs says

 bindValue(':PARAM1', null, PDO::PARAM_INT); 

or

 $myNull = null; $stmt->bindParam(':PARAM1', $myNull, PDO::PARAM_NULL); 
+1
source

if you want to bind params will have null values ​​then you need to use bindValues

 bindValue(':param', null, PDO::PARAM_INT); 

so for your request, it could be something like this.

 $sth = $dbh->prepare('SELECT field1 FROM table WHERE field2 = :PARAM1 AND fieldx = :PARAM1X'); $sth->bindValue(':PARAM1', null, PDO::PARAM_INT); 

hope this helps.

+1
source

Source: https://habr.com/ru/post/926461/


All Articles