Re: What is the difference between NULL and "undef"

From: "Michael A(dot) Mayo" <mmayo(at)mcauleybrooklyn(dot)org>
To: "Rudolph, Michael" <Michael(dot)Rudolph(at)telekom(dot)de>, "'Alessio Bragadini'" <alessio(at)albourne(dot)com>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: What is the difference between NULL and "undef"
Date: 2000-05-18 18:32:47
Message-ID: 003c01bfc0f7$78e51f40$1138b798@362197428
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

----- Original Message -----
From: "Rudolph, Michael" <Michael(dot)Rudolph(at)telekom(dot)de>
> But my original problem is not really solved
> I read in a date from an input-field of an formular. I now have to
> check, if this input-variable is empty. If it is, I set it to NULL,
> if not, I take it as input.

Unfortunately, there is no way to avoid testing for the empty string and
assigning it to NULL or undef. It would be annoying if the empty string
automatically translated to NULL, because it would be difficult to insert an
empty string into a database.

> My program looks now in parts like this:
> if ($var3 == 0){ $var3 = NULL } # Testing if empty
> else {$var3 = "'$var3'";} # input-field is not empty
> $rc = $dbh->do("INSERT INTO test1 (var1,var2,var3)
> VALUES ('$var1','$var2',$var3)");

You can also do this once for all the form variables instead of doing it
separately for each:
@fields = param();
foreach $current_field (@fields) {
if( param($current_field) eq "" ) {
param_fetch($current_field)->[0] = undef;
}
}

This assumes you are using CGI.pm, but a similar method should be possible
with the hash returned by ReadParse in cgi-lib or cgi_lite. As always,
there's more than one way to do it. =)

-Mike

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Kate Collins 2000-05-18 19:11:27 SQL command speed
Previous Message Brian C. Doyle 2000-05-18 18:01:37 Novice Help Needed