in reply to SELECT COUNT and DBI: rows

You can't use the rows method on select statements, only on statements that modify the database. In those cases, for most DBDs $dbh->do($sql) returns the number of rows affected. In other words, you can't use rows to count the rows from a select statement. This is in perldoc DBI in some detail, under the ->rows method.

Replies are listed 'Best First'.
Re: Re: SELECT COUNT and DBI: rows
by Cody Pendant (Prior) on Sep 28, 2003 at 23:46 UTC
    You can't use the rows method on select statements, only on statements that modify the database.

    OK, now I'm really confused. I do that all the time! I do a select, and use rows to check if it returned anything. Don't I? (Goes to check...) yes I do. I have code like this:

    ## prepare some SELECT statement $sth->execute() || die "Error: " . $dbh->errstr; if( $sth->rows > 0 ){ ## do some stuff }


    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
      OK, now I'm really confused. I do that all the time!

      It might work on whatever database you're using, but getting a row count back from a SELECT isn't portable.

        I'm using DBI and MySQL. That's interesting to know. I guess I should keep reading this thread to see what the more portable way to do it is...



        ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
Re: Re: SELECT COUNT and DBI: rows
by bart (Canon) on Sep 29, 2003 at 06:45 UTC
    You can't use the rows method on select statements
    Where on earth do you see the rows() method used? I don't. My browser can't find it.

    I have no idea why it doesn't work for the OP. It seems to be working for me.

Re: Re: SELECT COUNT and DBI: rows
by Anonymous Monk on Sep 29, 2003 at 14:43 UTC
    Mea culpa: I don't know where I saw the rows method used either, but obviously that was some good **** I was smoking yesterday. Please ignore that post, and I'll go find more to share. :I