2

I encountered a problem while fetching database field value which is basically a null value

I tried

SELECT * FROM table WHERE field=''; //this means null in php

and

SELECT * FROM table WHERE field IS NULL; //this means null in mysql db

Both queries are producing different result.

What is the difference b/w both null values and how can be the null values different?

2
  • The correct way to test for null in SQL is x IS NULL or x IS NOT NULL. This is just how it's defined. x = NULL should return false (for any x) per SQL standard, but has been broken in some SQL implementations. These semantics are useful for joins. Sadly, I don't have an answer of how to turn "passing a null" into IS NULL magically -- but this is trivial with prepared statements (oh, you are using those, no?) and a condition to select "which" query. However, I have not run into a case where I've needed to support NULL as such and am tempted to argue a [blank] not-NULL field. Commented Feb 26, 2011 at 7:51
  • Technically '' in PHP is an empty string it will evaluate to a boolean false, hence null == '' but only because the == operator does type casting. null === '' is false. Commented Apr 30, 2011 at 20:10

2 Answers 2

3

When querying the database, definitions from PHP are thrown out the window. In SQL, "" represents an empty string.

Sign up to request clarification or add additional context in comments.

Comments

2

In PHP programming, the difference between null, zero, false, and empty string easily becomes ambiguous because of PHP's == vs. ===. Oracle, MSSQL, and MySql (sometimes?) are a lot more strict in this respect.

PHP, as a high-level language, allows loose comparisons, as described by PHP type comparison tables.

1 Comment

This is exactly correct. Using ==, an empty string and null are equivalent. But they are indeed different values in PHP, and you can tell them apart using ===, is_string(), and is_null().

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.