faqts : Computers : Databases : MySQL : Language and Syntax : Queries : Number of Rows

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

17 of 22 people (77%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

Using Perl/DBI how can I check if SELECT returns no rows?

Jan 23rd, 2000 21:52
Nathan Wallace, Matt Wagner, Paul DuBois


Take a look at the DBI docs.

    my $query = $dbh->prepare(...);
    $query->execute or die "...";
    if ($query->rows == 0) {
        print "There were no matches.";
    }

Actually, the DBI docs say that you can't count on the rows method being
accurate for SELECT statements, and that you should count the rows as
you fetch them.

I often use logic something like this for the case in point:

    prepare
    execute
    count = 0
    while (fetch next row succeeds)
    {
        if count is 0 print any headers that are needed
        print row
          count
    }
    finish
    if count is 0
        print that there were no rows