By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,933 Members | 1,676 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,933 IT Pros & Developers. It's quick & easy.

Running MySQL script in Perl

P: 1
Hi All,
I'm having a problem executing a MySQL query witihin Perl and was hoping that someone will be able to help me in my moment of stress.

When I run the below script I get this error:

'Can't call method "prepare" on an undefined value at C:\Perl\Jimmy\Testing2.pl line 73'

Line 73 refers to 'e.badge as Badge, \'. The MySQL query runs fine in MySQL, but not sure where I'm going wrong in getting the Perl script to work. Any help would be greatly appreciated.

Jimmy

Expand|Select|Wrap|Line Numbers
  1. use DBI ;
  2. use MIME::Lite ;
  3. use Net::SMTP;
  4.  
  5. my @my_data;
  6. my ($msg_body, $my_table, $result) ;
  7.  
  8. sub OpenMySQL {
  9.  
  10. $dbh = DBI->connect('dbi:mysql:host=167.67.61.69;sid=esd','root','root',{RaiseError => 1, AutoCommit => 0});
  11. if ($dbh) {
  12.         # print "\tConnected!\n";    
  13.  
  14.     } else {
  15.          die "Cannot connect to MySQL" ;
  16.         }
  17. }
  18.  
  19.  
  20. sub CloseMySQL {
  21.  
  22.     $dbh->disconnect;
  23.     if ($dbh) {
  24.             # print "\tDisconnected\n";
  25.  
  26.         } else {
  27.             die "Could not disconnect";
  28.             }
  29. }
  30.  
  31.  
  32. sub RunQueries {
  33.  
  34.  $sth = $dbh->prepare("select distinct concat(e.LastName, ' ', e.Name) as Name, \
  35.              e.badge as Badge, \
  36.              curdate() as 'Date' \
  37.             FROM esduser e \
  38.             LEFT JOIN checkesd c on e.badge = c.badge\
  39.             and c.Date > curdate() \
  40.             where c.badge is null \
  41.             order by e.LastName");
  42.  
  43.   $sth->execute() || die "\tCould not execute query\n";
  44.   $result = $dbh->selectall_arrayref($sth);
  45.  
  46.   $sth->finish; 
  47.  
  48.  
  49.  
  50. }
  51.  
  52.  
Jan 19 '09 #1
Share this Question
Share on Google+
2 Replies


KevinADC
Expert 2.5K+
P: 4,059
Look in the DBI documentation to properly add error checking to your script. The problem appears to be that $sth is not getting defined. My SQL is too rusty to try and debug your query but it looks odd. What are all the '\' for in the query?
Jan 19 '09 #2

Icecrack
Expert 100+
P: 174
as Kevin said:

Why are you putting \ (Backslashes) in

Expand|Select|Wrap|Line Numbers
  1.  $sth = $dbh->prepare("select distinct concat(e.LastNa
  2. me, ' ', e.Name) as Name, \
  3.  
  4.              e.badge as Badge, \
  5.              curdate() as 'Date' \
  6.             FROM esduser e \
  7.             LEFT JOIN checkesd c on e.badge = c.badge\
  8.             and c.Date > curdate() \
  9.             where c.badge is null \
  10.             order by e.LastName");
  11.  
Perl does not need this it will read the whole (" ..... ") with out the \ like linux.
Jan 20 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.