code_junkie

How can I check if a database query will return results?

Nov 12th, 2011
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. sub list {
  2. require HTTP::Date;
  3. import HTTP::Date;
  4.  
  5. my $date = [split /s+/, HTTP::Date::time2iso(time())]->[0];
  6.  
  7. my $dbh = DBI->connect($dsn, $user, $password)
  8. || die "cannot connect to $database: $!n";
  9.  
  10. my $sql = <<EOSQL;
  11. SELECT * FROM $table where expiry >= '$date' order by expiry
  12. EOSQL
  13.  
  14. my $sth = $dbh->prepare($sql);
  15. $sth->execute();
  16.  
  17.  
  18. while (my $ref = $sth->fetchrow_hashref()) {
  19. my $temp = $template;
  20. $temp =~ s#__TITLE__#$ref->{'title'}#;
  21.  
  22. my $job_spec = $ref->{'job_spec'};
  23.  
  24. ...etc...
  25.  
  26. $sth->execute();
  27.  
  28. my $first = 1;
  29. while (my $ref = $sth->fetchrow_hashref()) {
  30. if( $first ) {
  31. print "We currently have the following vacancies:n";
  32. $first = 0;
  33. }
  34. my $temp = $template;
  35. ...
  36. }
  37. if( $first ) {
  38. print "No vacancies foundn";
  39. }
  40.  
  41. $sth->execute();
  42.  
  43. if($sth->rows) {
  44. print "We have data!n";
  45. }
  46.  
  47. while(my $ref = $sth->fetchrow_hashref()) {
  48. ...
  49. }
  50.  
  51. $sth->execute();
  52.  
  53. my $ref = $sth->fetchrow_hashref();
  54. if ($ref) {
  55. print "We currently have the following vacancies:n";
  56. while ($ref) {
  57. my $temp = $template;
  58. ...
  59. $ref = $sth->fetchrow_hashref();
  60. }
  61. } else {
  62. print "No vacancies foundn";
  63. }
  64.  
  65. my @results = ();
  66. while (my $ref = $sth->fetchrow_hashref()) {
  67. push @results, $ref;
  68. }
  69.  
  70. if ($#results == 0) {
  71. ... no results
  72. } else {
  73. foreach $ref (@results) {
  74. my $temp = $template;
  75. ....
  76. }
  77.  
  78. my $output = '';
  79.  
  80. $output .= "whatever we would have printed";
  81.  
  82. if ($output eq '')
  83. {
  84. print 'We have no vacancies.';
  85. }
  86. else
  87. {
  88. print "We currently have the following vacancies:n" . $output;
  89. }
  90.  
  91. use Lingua::EN::Inflect 'PL';
  92.  
  93. $sth->execute();
  94. my $results = $sth->fetchall_arrayref( {}, $max_rows );
  95.  
  96. if (@$results) {
  97. print "We currently have the following ", PL("vacancy",scalar @$results), ":n";
  98.  
  99. for my $ref (@$results) {
  100. ...
  101. }
  102. }
  103.  
  104. # count the vacancies
  105. $numinfo = $dbh->prepare("SELECT COUNT(*) FROM $table WHERE EXPIRY >= ?");
  106. $numinfo->execute($date);
  107. $count = $numinfo->fetchrow_arrayref()->[0];
  108.  
  109. # print a message
  110. my $msg = '';
  111. if ($count == 0) $msg = 'We do not have any vacancies right now';
  112. else $msg = 'We have the following vacancies';
  113. print($msg);
  114.  
  115. For a non-"SELECT" statement, "execute" returns the number of rows
  116. affected, if known. If no rows were affected, then "execute"
  117. returns "0E0", which Perl will treat as 0 but will regard as true.
  118.  
  119. my $returnval = $sth->execute;
  120. if (defined $returnval && $returnval == 0) {
  121. carp "Query executed successfully but returned nothing";
  122. return;
  123. }
Add Comment
Please, Sign In to add comment