Solved

Perl/Cron Error

Posted on 2004-09-15
7
235 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
  • 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now