http://www.perlmonks.org?node_id=239762

maksl has asked for the wisdom of the Perl Monks concerning the following question:

for a bigger cms i use frequently the following subroutine, which handles different sorts of sql queries, as my database days are still young and this part of code is essential for the cms i would like to your opionions about these lines.

sql_query handles different queries ranging from smaller and bigger select statements to update and insert like

my $chk_arid = "SELECT MAX( arid ) FROM $table where flag = ?"; my ($arid) = sql_query( $chk_arid , 1 ); # or my $cmd = "select aid, code from authors where name = ? "; ( $aid, $code ) = sql_query( $cmd, $name ); # or even my $cmd_up = "UPDATE SET flag = 0 where flag = 1 and arid = ?"; sql_query( $cmd_up, $pub{arid} ) or die("Error update flag"); my $cmd_ins = "INSERT INTO $table ( arid, catid, filename, title, aid, teaser, text, links, comment, changeby, newsid, keywords, description, flag) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; sql_query( $cmd_ins, $arid, $file, $pub{title}, $pub{aid}, $pub{teaser}, $pub{text}, $pub{links}, $comment, $aid, $pub{newsid}, $pub{keywords}, $pub{description}, 1 ) or die("Failed to insert new changed sql data :( \n");
the subroutine itself :))
sub sql_query { my $cmd = shift; my @place_holders; if ( defined $_[0] ) { foreach my $i ( 0 .. $_ ) { $place_holders[$i] = "$_[$i]"; } } die("Received no sql cmd for database query") unless ( defined +$cmd); my $dbh = DBI->connect("DBI:mysql:$dbname","$username","$passw +d"); die "ERR: Couldn't open connection: ".$DBI::errstr."\n" unless + $dbh; my $sth = $dbh->prepare( $cmd ); die "Couldn't prepare statement: $DBI::errstr stopped\n" unles +s $sth; $sth->execute( @place_holders ) or die "Couldn't execute statement: $DBI::errstr\n"; my @row = $sth->fetchrow_array() if ( $cmd =~ /SELECT/i ); $sth->finish; $dbh->disconnect; return @row if ( $cmd =~ /SELECT/i); return 1; }

Well everything is working so far, but are there any (small or big) relevant contraindication speeking against the use of this snippet?
thx in advance for your opinion and interessting pointers or cleanups maksl
(ps speed is not so a big limitation as this piece of code is only use for the authors .. users see plain htm)