At 1:11 PM -0500 8/24/99, gl3 wrote:
>Hi,
>
>I'm having a strange problem with update in a Perl script I have. When I
>try the following:
>
>$dbh = $sth->prepare("select $id from $table");
>$sth->execute;
>$yes = $sth->fetchrow;
>if ($yes ne "") {
> $dbh = $sth->prepare("update $table set q = "\$q\" where id =
>\"$id\"");
> $sth->execute;
>}
>else {
> $sth->prepare("insert into $table set id = \"$id\", set q =
>"\$q\"");
> $sth->execute;
>}
>
>it inserts a new id of the same value even if $yes eq "". I think my
>problem is with setting $yes, though I'm not sure. Should I be using
>replace instead?
If you want to determine whether or not such a row exists, then the
value of fetchrow will be undef (not "") if there is no such row.
You may want something like this:
if (defined ($yes)) # row exists
{
update
}
else # row does not exist
{
insert
}
--
Paul DuBois, paul@stripped