CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: DBI:
MySQL query problem

 



newera
Novice

Aug 6, 2015, 5:17 PM

Post #1 of 2 (5193 views)
MySQL query problem Can't Post

I'm trying to pull dat from 2 tables and then insert result into a third table. My code follows but only does 1 correct entry, and the rest are blank. There are 348 entries total. What am I missing here?


Code
$dbh = DBI->connect("DBI:mysql:$mysqldatabase:$mysqlhostname","$mysqlusername","$mysqlpassword"); 
if(!$dbh) { die("Error: could not get DBI handle\n"); }
$sqlConnect = 1;



$SQL =<<SQL;
SELECT * FROM oscmax2.info2
SQL

$sth = $dbh->prepare($SQL);
if(!$sth) { die("Error: " . $dbh->errstr . "\n"); }
if(!$sth->execute) { die("Error4: " . $sth->errstr . "\n"); }
while (my @row = $sth->fetchrow_array) {
$products_id = $FORM{'product_id'};
$affiliate_id = $FORM{'affiliate_id'};
$demo = $FORM{'demo'};
}

if($sth->rows != 0)
{
$total_rows = $sth->rows;

for ($counter = 0; $counter < $total_rows; $counter++)
{


$SQL =<<SQL;
SELECT products_attributes_id FROM oscmax2.products_attributes
WHERE products_id = '$products_id'
SQL

$sth = $dbh->prepare($SQL);
if(!$sth) { die("Error: " . $dbh->errstr . "\n"); }
if(!$sth->execute) { die("Error: " . $sth->errstr . "\n");}
while (my @row = $sth->fetchrow_array) {

$products_attributes_id = $FORM2{'products_attributes_id'};
}



$SQL =<<SQL;
INSERT INTO oscmax2.products_attributes_download(products_attributes_id,products_attributes_filename,products_attributes_maxdays,products_attributes_maxcount)
VALUES
('$products_attributes_id', '$affiliate_id/$demo', '7', '1')
SQL
$dbh->do($SQL) || die("Error5: " . $dbh->errstr . "\n");

}

}

$sth->finish();

if($sqlConnect) { $dbh->disconnect(); }



FishMonger
Veteran / Moderator

Aug 7, 2015, 7:47 AM

Post #2 of 2 (5187 views)
Re: [newera] MySQL query problem [In reply to] Can't Post

Add these 2 use statements (which should be in EVERY script) to the beginning of the script just after the shebang line.

Code
use strict; 
use warnings;

Those pragmas will point out lots of common coding mistakes/errors.

The strict pragma will require you to declare your vars, which is done with the my keyword.

Code
my $dbh = ....;


There are a number of problems with your code. To begin with, neither of the while loops do anything with the fetched values in the @row array, which makes them pointless.

The only thing those loops do is to repeatedly assign some variables the same value and those values have nothing to do with the fetched rows.


(This post was edited by FishMonger on Aug 7, 2015, 7:48 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives