Re: NULL value question
- From: Lawrence Statton <yankeeinexile@xxxxxxxxx>
- Date: 31 Jul 2007 14:18:49 -0500
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.
.
- References:
- NULL value question
- From: Jim James
- NULL value question
- Prev by Date: Re: CGI displays contents of script
- Next by Date: Re: CGI displays contents of script
- Previous by thread: Re: NULL value question
- Next by thread: Re: NULL value question
- Index(es):
Relevant Pages
|