Re: NULL value question



Jim James <jim@xxxxxxxxxxxxx> writes:

I'm coding my first web site using a mysql
database and DBI.

I can fetch records from the database
with no problem, but I'm having trouble
testing NULL values. If I load the value
of a date field into a variable ($dateadded),
how should the IF condition be written?

if ( $dateadded = NULL ) {

print "Date added is NULL";

}


unless ( defined ($dateadded) ) {
print 'Date added may have been NULL';
}

There are some edge cases you need to be wary of -- a DBI call may
return undef to show that the query failed ...

my $sth = $dbh->prepare(q(
SELECT dateadded, lunar_phase
FROM lunar_sighting
WHERE something IS TRUE
));

if (my ($dateadded, $lunar_phase) = $sth->fetchrow_array) {
if (defined($dateadded)) {
print "Lunar phase of $lunar_phase was sighted on $dateadded\n";
} else {
print "Lunar phase of $lunar_phase was sighted on NULL date\n";
}
}


--
Lawrence Statton - lawrenabae@xxxxxxxxxxxxx s/aba/c/g
Computer software consists of only two components: ones and
zeros, in roughly equal proportions. All that is required is to
place them into the correct order.
.



Relevant Pages

  • Balasan: RE: Balasan: RE: Balasan: RE: Balasan: RE: cant execute "use $database"
    ... dictionary of the each database of my MSSQL. ... to load it -- DBI will load it for you. ... Subject: Balasan: RE: Balasan: RE: Balasan: RE: ...
    (perl.dbi.users)
  • Re: How to use SQL to load supertype and subtype table data
    ... > I'm loading data into a database but having trouble (probably ... > then using that primary key as the Foreign Key to load data into my ... > very different local database ... > to use SQL and not a native API ...
    (comp.databases)
  • NULL value question
    ... database and DBI. ... I can fetch records from the database ... with no problem, but I'm having trouble ...
    (comp.lang.perl.misc)
  • Re: NULL value question
    ... I can fetch records from the database ... with no problem, but I'm having trouble ... If I load the value ...
    (comp.lang.perl.misc)
  • Re: Generating one table with a terabyte of data
    ... I didn't know we could split a database file group over multiple HDs. ... > Run perfmon counters to a log and analyze them after a bulk load. ... Another thing is to split the updates across file sets. ... >> loading of daily data in the fastest time. ...
    (microsoft.public.sqlserver.dts)