Current location: Hot Scripts Forums » Programming Languages » Perl » Script help


Script help

Reply
  #1 (permalink)  
Old 08-21-07, 01:50 PM
n00by n00by is offline
New Member
 
Join Date: Aug 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Script help

Hi everyone
Was wondering if anyone can help.
I am creating a array from a mySQL statement how would i go about seeing if the array is populated so that i can continue with a IF statement?

This is what i have done excuse the rough coding like i said im a newbie

perl Code:
  1. $cmdStr = ("SELECT date, bytes, destination FROM $tblname WHERE bytes <> '0' ORDER BY destination;");
  2.    $sth = $dbh->prepare($cmdStr) or die $DBI::errstr;
  3.    $sth->execute or die $DBI::errstr;
  4.    
  5.     my $main = $sth->fetchall_arrayref();
  6.    $sth->finish();
  7.    
  8.     $cmdCheck = ("SELECT SessionId FROM $tblname2;");
  9.    $sth2 = $dbh->prepare($cmdCheck) or die $DBI::errstr;
  10.    $sth2->execute or die $DBI::errstr;
  11.    
  12.     my $check = $sth2->fetchall_arrayref();
  13.    $sth2->finish();
  14.    
  15.    
  16.    
  17.     foreach my $get(@$check){
  18.        my ($sessionid) = @$get;
  19.        if ($sessionid = "")
  20.        {
  21.       foreach my $query(@$main){
  22.          my ($date, $bytes, $dest) = @$query;
  23.               $cmdStr=("INSERT INTO $tblname2 (date, user, sum_bytes) VALUES ('$date', '$dest', '$bytes');");
  24.          $sth1 = $dbh->prepare($cmdStr) or die $DBI::errstr;
  25.          $sth1->execute or die $DBI::errstr;   
  26.          $sth1->finish();
  27.          };
  28.        }
  29.        else
  30.        {
  31.       foreach my $query(@$main){
  32.          my ($date, $bytes, $dest) = @$query;
  33.          $cmdStr=("UPDATE $tblname2 SET date = '$date', sum_bytes = '$bytes', user = '$dest';");
  34.          $sth1 = $dbh->prepare($cmdStr) or die $DBI::errstr;
  35.          $sth1->execute or die $DBI::errstr;
  36.          $sth1->finish();
  37.          };
  38.        }
  39.        print "end";
  40.        }

It basically just checks if a value exists in the db and if it does it updates the values otherwise it inserts the values. Is there a easier way?
Thanks
G

Last edited by UnrealEd; 08-22-07 at 01:14 PM. Reason: please use the [highlight=perl] tag when posting perl code
Reply With Quote
  #2 (permalink)  
Old 08-21-07, 11:42 PM
curbview.com's Avatar
curbview.com curbview.com is offline
Junior Code Guru
 
Join Date: May 2006
Posts: 555
Thanks: 0
Thanked 0 Times in 0 Posts
On line 14 add the following to begin your if statement:

if ($check ne "") {

..YOUR CODE BLOCK...

}
Reply With Quote
  #3 (permalink)  
Old 08-22-07, 09:05 AM
n00by n00by is offline
New Member
 
Join Date: Aug 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the reply
I tried it though and it always takes the first statement even if there is data in the array. Basically i understand (ne "" ) as saying if the variable $check is empty then do ....

I updated it like this

perl Code:
  1. $cmdCheck = ("SELECT SessionId FROM $tblname2;");
  2.     $sth2 = $dbh->prepare($cmdCheck) or die $DBI::errstr;
  3.     $sth2->execute or die $DBI::errstr;
  4.    
  5.     my $check = $sth2->fetchall_arrayref();
  6.     $sth2->finish();
  7.    
  8.    
  9.     if ($check ne ""){
  10.         
  11.         print "One";
  12.         foreach my $query(@$main){
  13.             my ($date, $bytes, $dest) = @$query;
  14.                 $cmdStr=("INSERT INTO $tblname2 (date, user, sum_bytes) VALUES ('$date', '$dest', '$bytes');");
  15.             $sth1 = $dbh->prepare($cmdStr) or die $DBI::errstr;
  16.             $sth1->execute or die $DBI::errstr
  17.             $sth1->finish();
  18.             }
  19.         }
  20.         else {
  21.             print "Two";
  22.         foreach my $query(@$main){
  23.             my ($date, $bytes, $dest) = @$query;
  24.             $cmdStr=("UPDATE $tblname2 SET date = '$date', sum_bytes = '$bytes', user = '$dest';");
  25.             $sth1 = $dbh->prepare($cmdStr) or die $DBI::errstr;
  26.             $sth1->execute or die $DBI::errstr;
  27.             $sth1->finish();
  28.             }
  29.         }
  30.         print "end"
  31.         }

The "prints" are just a way for me to see what it is actually doing

Thanks again for the help.

Last edited by UnrealEd; 08-22-07 at 01:14 PM. Reason: please use the [highlight=perl] tag when posting perl code
Reply With Quote
  #4 (permalink)  
Old 08-22-07, 12:32 PM
curbview.com's Avatar
curbview.com curbview.com is offline
Junior Code Guru
 
Join Date: May 2006
Posts: 555
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by curbview.com View Post
On line 14 add the following to begin your if statement:

if ($check ne "") {

..YOUR CODE BLOCK...

}
Correction, that should be:

perl Code:
  1. $cmdCheck = ("SELECT SessionId FROM $tblname2;");
  2.     $sth2 = $dbh->prepare($cmdCheck) or die $DBI::errstr;
  3.     $sth2->execute or die $DBI::errstr;
  4.    
  5.     my $check = $sth2->fetchall_arrayref();
  6.     $sth2->finish();
  7.    
  8.    
  9.     if ($check eq ""){
  10.         
  11.         print "Value is empty... INSERTING data now.";
  12.         foreach my $query(@$main){
  13.             my ($date, $bytes, $dest) = @$query;
  14.                 $cmdStr=("INSERT INTO $tblname2 (date, user, sum_bytes) VALUES ('$date', '$dest', '$bytes');");
  15.             $sth1 = $dbh->prepare($cmdStr) or die $DBI::errstr;
  16.             $sth1->execute or die $DBI::errstr
  17.             $sth1->finish();
  18.             }
  19.         }
  20.         else {
  21.             print "Value not empty... UPDATING database records.";
  22.         foreach my $query(@$main){
  23.             my ($date, $bytes, $dest) = @$query;
  24.             $cmdStr=("UPDATE $tblname2 SET date = '$date', sum_bytes = '$bytes', user = '$dest';");
  25.             $sth1 = $dbh->prepare($cmdStr) or die $DBI::errstr;
  26.             $sth1->execute or die $DBI::errstr;
  27.             $sth1->finish();
  28.             }
  29.         }
  30.         print "end"
  31.         }

Last edited by curbview.com; 08-22-07 at 12:35 PM.
Reply With Quote
  #5 (permalink)  
Old 08-22-07, 10:09 PM
gobinll gobinll is offline
Newbie Coder
 
Join Date: Jan 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
hello..im new. my script if i upload to online server its ok..but on my computer which i configure apache2.2 and latest php5.2.X and mysgl 5.0 also phpmyadmin...
the script config file done edit follow my of line apache server needed..
the result comes out blank just whit scrin color only..please help me ...thanks
Reply With Quote
  #6 (permalink)  
Old 08-22-07, 11:17 PM
Drunken Perl Coder Drunken Perl Coder is offline
Wannabe Coder
 
Join Date: Aug 2006
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
this is the perl forum, not the php forum.
__________________
Mods: Please do not edit my posts to add syntax highlighting. Thank you.
Reply With Quote
  #7 (permalink)  
Old 08-23-07, 02:08 AM
n00by n00by is offline
New Member
 
Join Date: Aug 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks curbview. But it still doesnt work for me no matter what i say it always thinks the table has data in it even when i truncate the table. Maybe there is another way for me to do this? Any help would be appreciated...... i will have a look at my code again aswell.

Thanks again
Reply With Quote
  #8 (permalink)  
Old 08-23-07, 02:45 AM
curbview.com's Avatar
curbview.com curbview.com is offline
Junior Code Guru
 
Join Date: May 2006
Posts: 555
Thanks: 0
Thanked 0 Times in 0 Posts
PM me with what prints out.
perl Code:
  1. $cmdCheck = ("SELECT SessionId FROM $tblname2;");
  2.     $sth2 = $dbh->prepare($cmdCheck) or die $DBI::errstr;
  3.     $sth2->execute or die $DBI::errstr;
  4.    
  5.     my $check = $sth2->fetchall_arrayref();
  6.     $sth2->finish();
  7.    
  8.    
  9.     if ($check eq ""){
  10.         print "The value of CHECK is: \n $check\n\n";
  11.         sleep (2);
  12.         print "Value is empty... INSERTING data now.";
  13.         foreach my $query(@$main){
  14.             my ($date, $bytes, $dest) = @$query;
  15.                 $cmdStr=("INSERT INTO $tblname2 (date, user, sum_bytes) VALUES ('$date', '$dest', '$bytes');");
  16.             $sth1 = $dbh->prepare($cmdStr) or die $DBI::errstr;
  17.             $sth1->execute or die $DBI::errstr;
  18.             $sth1->finish();
  19.             }
  20.         }
  21.         else {
  22.             print "Value not empty... UPDATING database records.";
  23.         foreach my $query(@$main){
  24.             my ($date, $bytes, $dest) = @$query;
  25.             $cmdStr=("UPDATE $tblname2 SET date = '$date', sum_bytes = '$bytes', user = '$dest';");
  26.             $sth1 = $dbh->prepare($cmdStr) or die $DBI::errstr;
  27.             $sth1->execute or die $DBI::errstr;
  28.             $sth1->finish();
  29.             }
  30.         }
  31.         print "end"
  32.         }

Last edited by curbview.com; 08-23-07 at 02:52 AM.
Reply With Quote
  #9 (permalink)  
Old 09-02-07, 05:27 PM
curbview.com's Avatar
curbview.com curbview.com is offline
Junior Code Guru
 
Join Date: May 2006
Posts: 555
Thanks: 0
Thanked 0 Times in 0 Posts
Guess all is well. Sprinkling a few *print statements* will help you with understanding what's wrong...

Using the following solves 99% of any Perl coders problems too:

use strict;
use warnings;

And avoid eval statements!!!!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Submit button....can it send info to my email w/out the use of php???? lisa33 HTML/XHTML/XML 7 10-17-06 11:46 AM
3 Column CSS Fluid Layout (IE 6 Problem) Heidenreich12 CSS 9 10-04-06 03:22 PM
use html to open application absvinyl HTML/XHTML/XML 5 09-18-06 02:04 PM
CSS Border Width Question. nova912 CSS 6 09-07-06 09:13 AM


All times are GMT -5. The time now is 04:47 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.