bbfan has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
Read some posts here that were very similar to my goal, so came here seeking enlightenment. In short, am trying to move some DBI calls (prepare, execute) into a module, but the execution fails when calling "execute" (please see below). Would like to have both $dbh and $sth available to the CGI (e.g. outside of the module). The module snippet looks like this:
package My::DBU; use strict; use warnings; use diagnostics; use CGI::Carp; use DBI; sub new { my $class = shift; my $self = bless {}, $class; my $dbh = shift || return undef; $self->{dbh} = $dbh; return $self; } sub begin_work{ my $self = shift; $self->{dbh}->begin_work; } sub execute { my $self = shift; my $query = shift; my $sth = $self->{dbh}->prepare($query); my $rows = $sth->execute or die $DBI::errstr;; return \$sth; }
and the CGI snippet like this:
my $dbh = DBI->connect('DBI:mysql:db:localhost', ${dbuser},${dbpass}) or confess "$DBI::errstr<br>"; my $dbu = new My::DBU $dbh or confess "$DBI::errstr <br>"; $dbu->begin_work(); print "BEFORE ERR<br>"; my $sth = $dbu->execute( \$dbh, \$query, \$errmsg ); print "NEVER GETS HERE<br>";
with the error
You have an error in your SQL syntax; check the manual that correspond +s to your MySQL server version for the right syntax to use near 'REF( +0xaaa3700)' at line 1 at /usr/lib/perl5/site_perl/5.8.8/My/DBU.pm lin +e 96.\n
The query "select id from ${table}" is being used while attempting to resolve this issue. Links mentioned above are:
http://www.perlmonks.org/?node_id=324671 http://www.perlmonks.org/?node_id=870145
Many thanks for your time / suggestions.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: passing objects as references ?
by mr.nick (Chaplain) on Dec 08, 2011 at 15:37 UTC | |
by bbfan (Novice) on Dec 09, 2011 at 16:42 UTC | |
Re: passing objects as references ?
by Anonymous Monk on Dec 08, 2011 at 15:38 UTC | |
by mr.nick (Chaplain) on Dec 08, 2011 at 15:48 UTC | |
Re: passing objects as references ?
by locked_user sundialsvc4 (Abbot) on Dec 08, 2011 at 22:13 UTC | |
by Anonymous Monk on Dec 08, 2011 at 22:35 UTC |