Differentiate between NULL and 0

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GazK

    Differentiate between NULL and 0

    I have a script which takes the results of a form and updates or creates
    a record in a db table. So far so dull. It works fine, except when I
    leave certain fields blank; these are entered in the table as zero
    values. Can anyone assist me to differentiate between NULL and zero? the
    variables that are troubling are fatalities, injuries, latitude and
    longitude.

    TIA

    Garry

    code:

    # load variables into array
    $variables["type"] = $type;
    $variables["location"] = $location;
    $variables["fatalities "] = $fatalities;
    $variables["injuries"] = $injuries;
    $variables["latitude"] = $latitude;
    $variables["longitude"] = $longitude;
    $variables["developmen t"] = $development;
    $variables["list"] = $list;
    $variables["display"] = $display;
    $variables["descriptio n"] = $description;

    foreach ($variables as $key => $value) {
    # loop through variables;
    $element =& $variables[$key];
    if (isset($value)) {
    # if variable is not null, add quotes for sql query
    $element = "'" . $element . "'";
    } else {
    # variable is NULL - no quotes
    $element = "NULL";
    }
    unset($element) ;
    }

    # explode array back to variables
    extract($variab les);

    if(isset($event ID)) {
    # updating an existing record
    # create SQL from variables
    $sql = "
    UPDATE tblevents
    SET enumType = $type,
    locationID = $location,
    datDate = '$eventDate',
    intFatalities = $fatalities,
    intInjuries = $injuries,
    dblLatitude = $latitude,
    dblLongitude = $longitude,
    developmentID = $development,
    enumList = $list,
    enumDisplay = $display,
    txtDescription = $description
    WHERE eventID = $eventID
    ";
    } else {
    # creating a new record
    $sql = "
    INSERT INTO tblevents (
    eventID,
    enumType,
    locationID,
    datDate,
    intFatalities,
    intInjuries,
    dblLatitude,
    dblLongitude,
    developmentID,
    enumList,
    enumDisplay,
    txtDescription
    ) VALUES (
    NULL,
    $type,
    $location,
    '$eventDate',
    $fatalities,
    $injuries,
    $latitude,
    $longitude,
    $development,
    $list,
    $display,
    $description
    )
    ";
    }

    # execute SQL
    mysql_query($sq l) or die(mysql_error ());
  • askMe

    #2
    Re: Differentiate between NULL and 0


    GazK wrote:[color=blue]
    > I have a script which takes the results of a form and updates or creates
    > a record in a db table. So far so dull. It works fine, except when I
    > leave certain fields blank; these are entered in the table as zero
    > values. Can anyone assist me to differentiate between NULL and zero? the
    > variables that are troubling are fatalities, injuries, latitude and
    > longitude.
    >
    > TIA
    >
    > Garry
    >[/color]

    Zero is a number. NULL has no value assigned.



    Comment

    • GazK

      #3
      Re: Differentiate between NULL and 0

      askMe wrote:[color=blue]
      > GazK wrote:[color=green]
      >> I have a script which takes the results of a form and updates or creates
      >> a record in a db table. So far so dull. It works fine, except when I
      >> leave certain fields blank; these are entered in the table as zero
      >> values. Can anyone assist me to differentiate between NULL and zero? the
      >> variables that are troubling are fatalities, injuries, latitude and
      >> longitude.
      >>
      >> TIA
      >>
      >> Garry
      >>[/color]
      >
      > Zero is a number. NULL has no value assigned.
      >
      > http://www.askblax.com
      >[/color]

      Thanks, thats given me the answer; insert a test for
      is_numeric($var iable) on these items. Thanks again.

      Comment

      • Tim Van Wassenhove

        #4
        Re: Differentiate between NULL and 0

        On 2006-01-15, GazK <rubbish@rubbis h.com> wrote:[color=blue]
        > Thanks, thats given me the answer; insert a test for
        > is_numeric($var iable) on these items. Thanks again.[/color]

        if ($variable !== 0) {
        // ...
        }


        --
        Met vriendelijke groeten,
        Tim Van Wassenhove <http://timvw.madoka.be >

        Comment

        • Tony Marston

          #5
          Re: Differentiate between NULL and 0

          The easiest way I have found to test for the existence or non-existence of a
          value in the $_POST array, and to avoid any confusion between empty, null
          and zero, is to use the strlen() function. If the result is a length of
          zero, then it signifies no input. Easy peasy lemon squeezy.

          --
          Tony Marston

          This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL




          "GazK" <rubbish@rubbis h.com> wrote in message
          news:4MydnfGYSb 1woVfeRVny1Q@ec lipse.net.uk...[color=blue]
          >I have a script which takes the results of a form and updates or creates a
          >record in a db table. So far so dull. It works fine, except when I leave
          >certain fields blank; these are entered in the table as zero values. Can
          >anyone assist me to differentiate between NULL and zero? the variables that
          >are troubling are fatalities, injuries, latitude and longitude.
          >
          > TIA
          >
          > Garry
          >
          > code:
          >
          > # load variables into array
          > $variables["type"] = $type;
          > $variables["location"] = $location;
          > $variables["fatalities "] = $fatalities;
          > $variables["injuries"] = $injuries;
          > $variables["latitude"] = $latitude;
          > $variables["longitude"] = $longitude;
          > $variables["developmen t"] = $development;
          > $variables["list"] = $list;
          > $variables["display"] = $display;
          > $variables["descriptio n"] = $description;
          >
          > foreach ($variables as $key => $value) {
          > # loop through variables;
          > $element =& $variables[$key];
          > if (isset($value)) {
          > # if variable is not null, add quotes for sql query
          > $element = "'" . $element . "'";
          > } else {
          > # variable is NULL - no quotes
          > $element = "NULL";
          > }
          > unset($element) ;
          > }
          >
          > # explode array back to variables
          > extract($variab les);
          >
          > if(isset($event ID)) {
          > # updating an existing record
          > # create SQL from variables
          > $sql = "
          > UPDATE tblevents
          > SET enumType = $type,
          > locationID = $location,
          > datDate = '$eventDate',
          > intFatalities = $fatalities,
          > intInjuries = $injuries,
          > dblLatitude = $latitude,
          > dblLongitude = $longitude,
          > developmentID = $development,
          > enumList = $list,
          > enumDisplay = $display,
          > txtDescription = $description
          > WHERE eventID = $eventID
          > ";
          > } else {
          > # creating a new record
          > $sql = "
          > INSERT INTO tblevents (
          > eventID,
          > enumType,
          > locationID,
          > datDate,
          > intFatalities,
          > intInjuries,
          > dblLatitude,
          > dblLongitude,
          > developmentID,
          > enumList,
          > enumDisplay,
          > txtDescription
          > ) VALUES (
          > NULL,
          > $type,
          > $location,
          > '$eventDate',
          > $fatalities,
          > $injuries,
          > $latitude,
          > $longitude,
          > $development,
          > $list,
          > $display,
          > $description
          > )
          > ";
          > }
          >
          > # execute SQL
          > mysql_query($sq l) or die(mysql_error ());[/color]


          Comment

          Working...