Solved

Perl/Cron Error

Posted on 2004-09-15
7
240 Views
Last Modified: 2008-02-26
Hi all,
Thanks in advance for your help.

I keep getting this error:
DBD::mysql::st fetchrow_array failed: fetch() without execute() at /home/a1classi/public_html/warn.cgi line 57

Here is the code it is suppose to send out a warning email:

#!/usr/bin/perl

use CGI;
use DBI;


$host = "localhost";
$port = "****";
$db_name = "****";
$mysqlusername = "****";
$mysqlpassword = "****";
$sema = "/usr/sbin/sendmail";
############WS HITS############



####GETS MYSQL DATABASE#########
$dbh = DBI->connect("DBI:mysql:database=$db_name;host=$host;port=$port",$mysqlusername,$mysqlpassword) ||  die "cannot connect to database";

$t = time;





####GETS PEOPLE TO SEND EMAIL TO#########
$query = "SELECT email, idnum, password FROM classifiedads WHERE expires < $t AND warned = 0";
$sth = $dbh->prepare($query);
$sth->execute;



while(@row = $sth->fetchrow_array) {

####MARKS THE ACCOUNT AS HAVING BEEN WARNED#########
$sth = $dbh->prepare("UPDATE classifiedads SET warned = '1' WHERE idnum = '$row[1]'");
$sth->execute;
$sth->finish();
                              
                              
$expiredmess = "email message";
            
####SENDS EMAIL#########            
    open (MESSAGE,"|$sema -t");
    print MESSAGE "To: $row[0]\n";
    print MESSAGE "From: support\@\n";
    print MESSAGE "Subject: message\n\n";
    print MESSAGE "$expiredmess\n\n";
    close (MESSAGE);


            }
            
            
            
exit;
0
Comment
Question by:cortecds
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 10

Expert Comment

by:Jay Toops
ID: 12076923
This is suspect
$query = "SELECT email, idnum, password FROM classifiedads WHERE expires < $t AND warned = 0";
$sth = $dbh->prepare($query);
$sth->execute;

$t is probably returning somthing that can't be interpreted as a date or
maybee even results in invalid sql

JAY
0
 

Author Comment

by:cortecds
ID: 12078118
Hi,
Thanks for your help the $t appears to work because I guess I failed to mention that the script works it just emails me that error.

So if I have an account that is expiring then it finds it and marks it as warned and sends out the warning.

But it also tells me:
DBD::mysql::st fetchrow_array failed: fetch() without execute() at /home/a1classi/public_html/warn.cgi line 57

But line 57 is actually the  liner that says:
close (MESSAGE);


Thanks!
0
 
LVL 10

Expert Comment

by:Jay Toops
ID: 12079029
maybee you need to flush the message first??

Jay
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cortecds
ID: 12079123
Not sure what you mean by flush
0
 
LVL 1

Accepted Solution

by:
elmic earned 500 total points
ID: 12095370
The problem I see is that your redeclaring the STH in the middle of the fetch of STH:

while(@row = $sth->fetchrow_array) {
     ####MARKS THE ACCOUNT AS HAVING BEEN WARNED#########
>     $sth = $dbh->prepare("UPDATE classifiedads SET warned = '1' WHERE idnum = '$row[1]'");
>     $sth->execute;
>    $sth->finish();


}

Change the inner STH to somthing else like STHA. Here an example, I also put a my declare which is a good habit you might want to start doing:

while(@row = $sth->fetchrow_array) {

      ####MARKS THE ACCOUNT AS HAVING BEEN WARNED#########
      my $stha = $dbh->prepare("UPDATE classifiedads SET warned = '1' WHERE idnum = '$row[1]'");
      $stha->execute;
      $stha->finish();

      $expiredmess = "email message";
      
      ####SENDS EMAIL#########          
      open (MESSAGE,"|$sema -t");
      print MESSAGE "To: $row[0]\n";
      print MESSAGE "From: support\@\n";
      print MESSAGE "Subject: message\n\n";
      print MESSAGE "$expiredmess\n\n";
      close (MESSAGE);
}
0
 

Author Comment

by:cortecds
ID: 12103121
DUH :P I can't believe I didn't catch that.
Sometimes you can't see the forrest through the trees.
THANKS!
0
 
LVL 1

Expert Comment

by:elmic
ID: 12128905
Happens to all of us.... :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question