Let's have 3 records in the database:
id | name | value
---|-------|--------
1 | 'xxx' | 'zzz'
2 | null | 'aaa'
3 | 'yyy' | 'bbb'
There is a simple data query I want to perform. It is easy to find out if the following object is in database:
name = 'yyy'
value = 'bbb'
But what about the others? They contain null values. Is there any way to tell PDO to use WHERE name IS NULL or WHERE value IS NULL when the respective field is actually null? I have seen similar functionality in several frameworks but I do not know how to do this directly in PDO. Is it even possible or we have to check whether the values are null and build the query according to it?
$pdo->query('select id from my_table where name=? and value=?', array(null, 'aaa'));WHERE column = NULLis incorrect. It's calledWHERE column IS NULL.WHERE column IS 'value'is incorrect as well though, so that's why OP is asking, as you can't just bind NULL.<=>operator which works with nulls as well. Only need to bind value dynamically withPARAM_TYPE_NULL.