Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: if (empty record set)

by tcf22 (Priest)
on Jun 26, 2003 at 14:59 UTC ( [id://269258]=note: print w/replies, xml ) Need Help??


in reply to if (empty record set)

Using $sth->rows() is the answer if you need to actually work with the data, but if you just need to see if there are rows that match, consider using: (untested)
my $SQLString "select COUNT(1) from there"; $sth = $dbh->prepare($SQLString); $sth->execute(); @bkmark = $sth->fetchrow; if($bkmark[0] == 0) { ........... }

Replies are listed 'Best First'.
Re: Re: if (empty record set)
by CountZero (Bishop) on Jun 26, 2003 at 16:13 UTC

    If you are working with mySQL SELECT COUNT (*) FROM ... is particularly fast, the engine being optimised for such things.

    So if you just want to know if there are any records available, thats the way to do it, rather than fetching the records themselves. If you need the data also, of course it is better to go with the other suggestions.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Re: if (empty record set)
by Grygonos (Chaplain) on Jun 26, 2003 at 15:10 UTC
    on many RDBMS the execute method will return the number of rows like so:
    my $query = "SELECT * FROM myTable"; $sth = $dbh->prepare($query); if(!$sth->execute()) { ... }
    it varies from system to system .. but is another way to accomplish what you want

    edit: you could also run this query
    SELECT Count(yourPrimaryKeyHere) FROM yourTable
    if you're on a database that doesn't support execute's return value or $sth->rows
      Thats not the way execute works in DBI. Check the docs, but to summarise for you now
      If an error occurs you get undef back
      If it doesn't know the number of rows affected get -1 back
      If no rows are affected you will get 0E0 back.
      otherwise you get the number of rows affected
        The last time I tried it on MySQL it worked like that... but that was a while ago things may have changed...

        hope my old company doesn't upgrade their DBI because I used that return value from execute on a number of scripts which worked fine under that version of dbi... Thanks for the update.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://269258]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-26 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found