[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Savannah-dev] Re: DBI
From: |
Mathieu Roy |
Subject: |
[Savannah-dev] Re: DBI |
Date: |
30 May 2003 12:24:55 +0200 |
User-agent: |
Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 |
"Jaime E. Villate" <address@hidden> a tapoté :
> 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.
In fact, as it was not working, I tried with adding the connect/disconnect
sequence directly in the sub - which basically I do not want to do.
But since it was not working anyway (because probably of the missing
declaration), I removed the connect and apparently forgot to remove
the disconnect step. It would have been problematic for the next
steps, but as I was running the script with a exit; directly after
where it was failing, it was not an issue.
A strict "vars"; would have saved me a lot of time. This is really a
stupid mistake.
--
Mathieu Roy
Homepage:
http://yeupou.coleumes.org
Not a native english speaker:
http://stock.coleumes.org/doc.php?i=/misc-files/flawed-english