1

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?

14
  • possible duplicate of Bind IS NULL or NULL when using PHP PDO and MySql Commented Sep 25, 2013 at 14:41
  • Have you tried? You can just add in the WHERE clause just like you have it. What's your full query? Commented Sep 25, 2013 at 14:46
  • I have tried. This does not work: $pdo->query('select id from my_table where name=? and value=?', array(null, 'aaa')); Commented Sep 25, 2013 at 14:48
  • @aynber The problem is that WHERE column = NULL is incorrect. It's called WHERE 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. Commented Sep 25, 2013 at 14:54
  • 2
    @Pavel you can use the <=> operator which works with nulls as well. Only need to bind value dynamically with PARAM_TYPE_NULL. Commented Sep 25, 2013 at 16:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.