[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cgiapp] Strange mysql behaviour


BARKER, Paul wrote:
Hi All

I'm back using CGI::Application after what must be a year or more away doing
other stuff so feel free to flame me (gently!) if this is an obvious mistake
but ...

I'm writing a small CGI::Application based project. I have my runmodes
defined thus:

sub setup
{
    my $self = shift;
    $self->run_modes({
                      menu      =>  "menu",        # Initial menu page
                      error     =>  "print_error", # Error handler
                      hostinfo  =>  "host_info",   # Host information page
                      imgindex  =>  "img_index",   # Images index
                    });
$self->start_mode('menu');
    $self->param('dbh' => DBI->connect('dbi:mysql:platform', 'webadm',
'web123', {PrintError => 0})); # Store a db handle for later
}

The problem I have is in the img_index sub. I've cut it back to the following
:

sub img_index
# Show index of images for a particular hostid
{
    my $self   = shift;
    my $query  = $self->query();
    my $hostid = $query->param('host');
    my $ouptut = '';
    my $dbh    = $self->param('dbh');
my $hostname = $dbh->selectrow_array("SELECT hostname FROM hosts WHERE
hostid=?", undef, $hostid);
}

It seems that anytime I do a $dbh->prepare (which is explicitly called inside
the DBI selectrow_array method) I get the output of the prepare in the
browser and nothing else.

If I access the above via
http://myhost/cgi-perl/AEPWeb.pl?rm=imgindex&host=00004 I get the word
'aurora' in the browser. Now while this is the right answer since I'm not
actually asking it to output I'm a little confused as to how it gets there.

If I write something like $dbh->prepare("SELECT updated, updatedby FROM
images WHERE hostid=?"); I get something along the lines of
DBI::st=HASH(xxxxxx) in the browser.

Does anyone have any idea why this is happening ?

Yeah, C::A will take what ever is returned from your runmode and print it (after printing the headers) if don't do an explicit return, perl will simple take the value of the last expression and return it. If you end your sub with an assignment, then value of that expression is the value that was assigned (in your example 'aurora') so C::A will print this.

You prepare() will return the DBI statement object so that's what you run mode returns and hence what C::A prints.

You should use explicit returns of you output if you want to avoid this confusion.

HTH

--
Michael Peters
Developer
Plus Three, LP


---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
             http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.