How do I know how many rows a Perl DBI query returns?

后端 未结 7 859
遥遥无期
遥遥无期 2020-12-15 18:51

I\'m trying to basically do a search through the database with Perl to tell if there is an item with a certain ID. This search can return no rows, but it can also return one

7条回答
  •  猫巷女王i
    2020-12-15 19:21

    my $th = $dbh->prepare(qq{SELECT bi_exim_id FROM bounce_info WHERE bi_exim_id = '$exid'});
    $th->execute();
    my $found = 0;
    while ($th->fetch()) 
    {
       $found = 1;
    }
    

    Your query won't return anything if the row doesn't exist, so you can't de-reference the fetch.

    Update: you might want to re-write that as

    my $found = $th->fetch();
    

提交回复
热议问题