Solved

Perl/Cron Error

Posted on 2004-09-15
7
239 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…

730 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