Solved

Perl/Cron Error

Posted on 2004-09-15
7
237 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl output collect 7 67
Search in text file in column and compare 4 47
Regex rule to match two different url 5 71
Export Variables in Perl 3 67
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.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 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