mysql and null values

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hello, quick question about MySQL storing NULL values...

Say I have a textbox called $_POST["text"] and a variable $var.

    $var = NULL;
    $var = $_POST["text"];

Disregarding filtering/formatting the data, upon inserting $var into
MySQL, I thought it *should* result in a NULL entry in the database (the
field is setup to accept NULL values). I am sure $var = NULL before
inserting into the table because is_null() returns TRUE.

The field is of type CHAR and the entry gets added simply as ''
(obviously without the quotes). If I run a select statement to find all
entries where field = NULL, I get 0, but where field = '' returns the entry.

I don't understand why it's doing this. The only way I can get it to say
NULL in the record is by actually setting $var = 'NULL', but that to the
best of my knowledge is just setting $var to the CHAR value 'NULL', not
actually NULL.

Thanks in advance for all your help.


Re: mysql and null values

I beleive the main problem is that MySQL has a special syntax when
setting something to NULL.  Simply assigning it will not work.  I would
suggest you look here -"> .  Maybe in
the PHP you should set it to the string 'NULL' and then in the SQL make
sure it is not quoted.

quote from the manual:
* If you want to search for column values that are NULL, you cannot use
* expr = NULL test. The following statement returns no rows, because
* expr = NULL is never true for any expression:
* mysql> SELECT * FROM my_table WHERE phone = NULL;
* To look for NULL values, you must use the IS NULL test. The following

* statements show how to find the NULL phone number and the empty phone
* mysql> SELECT * FROM my_table WHERE phone IS NULL;
* mysql> SELECT * FROM my_table WHERE phone = '';

Marcus wrote:
Quoted text here. Click to load it
Quoted text here. Click to load it

Site Timeline