Helpful Information
 
 
Category: Perl Programming
Select multiple rows

I have a table which contains 3 records.
When I display the total number of rows selected, it is 3.
But when I try inserting into another table, it only insert one row.

Can anybody please help me.


$sth = $dbh->prepare("SELECT id FROM tab1 WHERE parentid = '1'");

$sth->execute()
$rv = $sth->rows;

my @row;
while ( @row = $sth->fetchrow_array( ) ) {
$sth = $dbh->prepare("INSERT INTO tab2(id) VALUES ('@row')");

$sth->execute()
}

Well 1st of all, there's no need to recreate the prepare statement every time, that's why you use a prepare statment.

$sth = $dbh->prepare("INSERT INTO tab2(id) VALUES ('?')");
while ( my @row = $sth->fetchrow_array( ) ) {
$sth->execute(@row);
}

That's not tested, but give it a try.










privacy (GDPR)