
08-21-07, 01:50 PM
|
|
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:
$cmdStr = ("SELECT date, bytes, destination FROM $tblname WHERE bytes <> '0' ORDER BY destination;"); $sth = $dbh-> prepare($cmdStr) or die $DBI:: errstr; $sth-> execute or die $DBI:: errstr; my $main = $sth->fetchall_arrayref(); $sth->finish(); $cmdCheck = ("SELECT SessionId FROM $tblname2;"); $sth2 = $dbh-> prepare($cmdCheck) or die $DBI:: errstr; $sth2-> execute or die $DBI:: errstr; my $check = $sth2->fetchall_arrayref(); $sth2->finish(); foreach my $get(@$check){ my ($sessionid) = @$get; if ($sessionid = "") { foreach my $query(@$main){ my ($date, $bytes, $dest) = @$query; $cmdStr=("INSERT INTO $tblname2 (date, user, sum_bytes) VALUES ('$date', '$dest', '$bytes');"); $sth1 = $dbh-> prepare($cmdStr) or die $DBI:: errstr; $sth1-> execute or die $DBI:: errstr; $sth1->finish(); }; } else { foreach my $query(@$main){ my ($date, $bytes, $dest) = @$query; $cmdStr=("UPDATE $tblname2 SET date = '$date', sum_bytes = '$bytes', user = '$dest';"); $sth1 = $dbh-> prepare($cmdStr) or die $DBI:: errstr; $sth1-> execute or die $DBI:: errstr; $sth1->finish(); }; } }
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
|

08-21-07, 11:42 PM
|
 |
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...
}
|

08-22-07, 09:05 AM
|
|
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:
$cmdCheck = ("SELECT SessionId FROM $tblname2;"); $sth2 = $dbh-> prepare($cmdCheck) or die $DBI:: errstr; $sth2-> execute or die $DBI:: errstr; my $check = $sth2->fetchall_arrayref(); $sth2->finish(); if ($check ne ""){ foreach my $query(@$main){ my ($date, $bytes, $dest) = @$query; $cmdStr=("INSERT INTO $tblname2 (date, user, sum_bytes) VALUES ('$date', '$dest', '$bytes');"); $sth1 = $dbh-> prepare($cmdStr) or die $DBI:: errstr; $sth1-> execute or die $DBI:: errstr; $sth1->finish(); } } else { foreach my $query(@$main){ my ($date, $bytes, $dest) = @$query; $cmdStr=("UPDATE $tblname2 SET date = '$date', sum_bytes = '$bytes', user = '$dest';"); $sth1 = $dbh-> prepare($cmdStr) or die $DBI:: errstr; $sth1-> execute or die $DBI:: errstr; $sth1->finish(); } } }
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
|

08-22-07, 12:32 PM
|
 |
Junior Code Guru
|
|
Join Date: May 2006
Posts: 555
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by curbview.com
On line 14 add the following to begin your if statement:
if ($check ne "") {
..YOUR CODE BLOCK...
}
|
Correction, that should be:
perl Code:
$cmdCheck = ("SELECT SessionId FROM $tblname2;"); $sth2 = $dbh-> prepare($cmdCheck) or die $DBI:: errstr; $sth2-> execute or die $DBI:: errstr; my $check = $sth2->fetchall_arrayref(); $sth2->finish(); if ($check eq ""){ print "Value is empty... INSERTING data now."; foreach my $query(@$main){ my ($date, $bytes, $dest) = @$query; $cmdStr=("INSERT INTO $tblname2 (date, user, sum_bytes) VALUES ('$date', '$dest', '$bytes');"); $sth1 = $dbh-> prepare($cmdStr) or die $DBI:: errstr; $sth1-> execute or die $DBI:: errstr; $sth1->finish(); } } else { print "Value not empty... UPDATING database records."; foreach my $query(@$main){ my ($date, $bytes, $dest) = @$query; $cmdStr=("UPDATE $tblname2 SET date = '$date', sum_bytes = '$bytes', user = '$dest';"); $sth1 = $dbh-> prepare($cmdStr) or die $DBI:: errstr; $sth1-> execute or die $DBI:: errstr; $sth1->finish(); } } }
Last edited by curbview.com; 08-22-07 at 12:35 PM.
|

08-22-07, 10:09 PM
|
|
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
|

08-22-07, 11:17 PM
|
|
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.
|

08-23-07, 02:08 AM
|
|
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 
|

08-23-07, 02:45 AM
|
 |
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:
$cmdCheck = ("SELECT SessionId FROM $tblname2;"); $sth2 = $dbh-> prepare($cmdCheck) or die $DBI:: errstr; $sth2-> execute or die $DBI:: errstr; my $check = $sth2->fetchall_arrayref(); $sth2->finish(); if ($check eq ""){ print "The value of CHECK is: \n $check\n\n"; print "Value is empty... INSERTING data now."; foreach my $query(@$main){ my ($date, $bytes, $dest) = @$query; $cmdStr=("INSERT INTO $tblname2 (date, user, sum_bytes) VALUES ('$date', '$dest', '$bytes');"); $sth1 = $dbh-> prepare($cmdStr) or die $DBI:: errstr; $sth1-> execute or die $DBI:: errstr; $sth1->finish(); } } else { print "Value not empty... UPDATING database records."; foreach my $query(@$main){ my ($date, $bytes, $dest) = @$query; $cmdStr=("UPDATE $tblname2 SET date = '$date', sum_bytes = '$bytes', user = '$dest';"); $sth1 = $dbh-> prepare($cmdStr) or die $DBI:: errstr; $sth1-> execute or die $DBI:: errstr; $sth1->finish(); } } }
Last edited by curbview.com; 08-23-07 at 02:52 AM.
|

09-02-07, 05:27 PM
|
 |
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!!!!
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|