savannah-dev
[Top][All Lists]
Advanced

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

Re: [Savannah-dev] DBI


From: Jaime E. Villate
Subject: Re: [Savannah-dev] DBI
Date: Fri, 30 May 2003 11:07:54 +0100
User-agent: Mutt/1.2.5i

On Thu, May 29, 2003 at 07:53:05PM +0200, Mathieu Roy wrote:

> Does anyone know why with a function like this
> 
> sub GetDB {
>     my $table = $_[0];
>     my $criterion;
>     my $fields = "*";
>     my $hop;
> 
>     $criterion = "WHERE ".$_[1] if $_[1];
>     $fields = $_[2] if $_[2];
> 
>     $hop = $dbd->prepare("SELECT ".$fields." FROM ".$table." ".$criterion);
>     $hop->execute;
>     while (my (@line) = $hop->fetchrow_array) {
>         push(@ret, join(",", map {defined $_ ? $_ : "0"} @line));
>         
>     }
>     $hop->finish;
>     $dbd->disconnect;
>     return @ret;
> }
> 
> The second time I run GetDB() in a script, it returns the result of
> the new request but also the result of the old - independantly of the
> fact that the two requests are different?

Hi Mathieu,
as Derek already told you, you need a "my @ret;" statement at the
beginning of the function.

However, I also notice something very odd. You don't use a DBI->connect
statement in the function, which means that when you call it, the connection
to the database has already been stablished. However, you use
$dbd->disconnect; inside the function, to close the database connection.

I think it is better to keep the connection and disconnection statements in
the same block. Either you connect, call GetDB and disconnect after that call,
or you call GetDB and let it connect and disconnect from the database.

Cheers,
Jaime




reply via email to

[Prev in Thread] Current Thread [Next in Thread]