Hallo!
Mein momentantes Script sieht so aus:
================================================================
#!/usr/bin/perl
use DBI;
use strict;
use CGI;
my $cgi = CGI->new();
print "Content-type: text/plain\n\n";
my $dbh = DBI->connect('DBI:mysql:database=foobar;host=localhost', '', '') || die $dbh->errstr;
my $sth = $dbh->prepare("INSERT INTO customers VALUES (?, ?)") || die $dbh->errstr;
$sth->execute('', 'User') || die $sth->errstr;
$sth->finish;
$dbh->disconnect;
================================================================
DBI.pm sagt zu $sth->finish;
Indicate that no more data will be fetched from this statement handle
before it is either executed again or destroyed. The finish method
is rarely needed, and frequently overused, but can sometimes be
helpful in a few very specific situations to allow the server to free
up resources (such as sort buffers).
================================================================
Meine Frage:
Wenn ich nicht nur einmal was einfügen will,
sondern im selben script noch was SELECT'ieren will,
muss ich dann das $sth->finish dann trotzdem an dieser
Stelle schreiben wo es jetzt ist und danach nochmals
$sth->prepare(...) machen,
oder darf ich das $sth->finish; erst vor dem disconnect
schreiben?
Nehmen wir der einfachheit noch ein INSERT an auch wenn
das beispiel blöd ist und mit $dbh->do gehen würde,
es geht nur ums testen:
============================================================
#!/usr/bin/perl
use DBI;
use strict;
use CGI;
my $cgi = CGI->new();
print "Content-type: text/plain\n\n";
my $dbh = DBI->connect('DBI:mysql:database=foobar;host=localhost', '', '') || die $dbh->errstr;
my $sth = $dbh->prepare("INSERT INTO customers VALUES (?, ?)") || die $dbh->errstr;
$sth->execute('', 'User') || die $sth->errstr;
############################################
$sth->finish; ### HIER LASSEN ODER WEG ???
#############################################
$sth = $dbh->prepare("INSERT INTO foobar VALUES (?, ?, ?, ?)") || die $dbh->errstr;
$sth->execute('', 'a', 'b', 'c') || die $sth->errstr;
$sth->finish;
$dbh->disconnect;
============================================================
2 Fragen dazu:
- Gehört das $sth->finish; da dazwischen rein oder nicht?
- da sind zwei mal $sth = $dbh->prepare() drinnen
und auch doppelt das $sth->execute.
Sollte ich beim 2. mal andere namen verwenden wie
$sth_2 und $dbh_2 damit das keine Probleme macht
weil da oben schonmal $dbh und $sth verwendet wurde??
(Bezieht sich aufs 2. Statement UNTER $sth->finish;)
Vielen lieben Dank
Aqua