Jump to content

Null? Whats that mean?


jwk811

Recommended Posts

you would allow null values for fields that the user does not HAVE to fill in...

I.E. when filling out a form, there are often "required" fields, which would not allow a null value, and fields which are optional for the user to fill out, which will allow null values
Link to comment
Share on other sites

But, be careful here. With repsect to programming, different languages treat NULL differently.

NULL does not equal no value. In a true sense NULL means that there never was a value.

Here's an example. Suppose you had a form with two fields where a person could enter their first name and last name. Now, let's suppose the user enters a firstname, but not a lastname. If you used this code to enter the data:

[code]<?php
$sql = "INSERT INTO 'table' (fname, lname) VALUES(".$_POST[fname].", ".$_POST[lname].")";
?>[/code]

Then the value for lastname would NOT be null. It would have a value of an empty string. However if you used this code:

[code]<?php
$fields .= "fname";
$values .= $_POST[fname];
if ($_POST[lname] != "") {
  $fields .= ", lname";
  $values .= ", " . $_POST[lname];
}

if ($_POST[lname]) {}
$sql = "INSERT INTO 'table' (".$fields.") VALUES(".$values.")";
?>[/code]

Then there would be NO value inserted into the last name field. It doesn't have a value because it never existed. It's a very subtle, but important difference.
Link to comment
Share on other sites

A critical difference between php's null and sql's null is that sql's null is not equal to itself.

$a = null; $b = null; if ($a === $b) print "null === null\n";

This will show that php null is equal to php null.  But

SELECT null = null;

in SQL will give you the result "false".  SQL's null is never equal to itself, because it represents "unknown value".

That's why you have to use "IS null" or "IS NOT null" to test for nullness in SQL, instead of using "= null".

However, people very often use null in sql to mean "no value" as well as "unknown value".  So whichever meaning you are using it for, you need to use the appropriate conditions.  Typically you'll use "IS null" when null means "no value", but you'll check for equality when it means "unknown value".
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.