Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Re: Re: If no results come back ...

by ehdonhon (Curate)
on Jul 21, 2002 at 03:31 UTC ( [id://183745]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: If no results come back ...
in thread If no results come back ...

Ahh.. you have stumbled upon the other reason for being concise, so people know what you are asking for. :)

If you want to know if there are any results, check the return value from $sth->execute. But you have to be careful since 0 results returns "0E0" (zero, but true). That trick is what prevents the 'or die' part of your command from running if no results return. Basically, you just need to know to evaluate the return value from the execute command in a numeric context. Also, if you need to know the result of that query to decide if you should print something, then obviously you need to run the query before you try to print it..

my $rows; if($id) { $sth = $dbh->prepare("SELECT id,name FROM category WHERE parent = ' +$id'"); $sth->execute or die $dbh->errstr; } else { $sth = $dbh->prepare("SELECT id,name FROM category WHERE parent = ' +0'"); $rows = $sth->execute or die $dbh->errstr; } if ( $rows + 0 ) { print qq~ ... ~; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-23 15:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found