Leser: 1
5 Einträge, 1 Seite |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
require DBI; # laden des DBI-Moduls
my $db_user = '';
my $db_passwd = '';
my $dsn = 'DBI:mysql:spiel:localhost'; # datasourcename: wenn myserver weggelassen wird,
# wird standardmäßig localhost verwendet.
my $attributes = { PrintError => 0, RaiseError => 0, "AutoCommit" => 1, }; # siehe Doku zu DBI
my $dbh = DBI->connect($dsn, $db_user, $db_passwd, $attributes)
#or main::writeDbhError($dbh, "Error in connecting to $dsn", die => 1);
or main::writeDbhError("Error in connecting to $dsn", die => 1);
# immer Returncodes und Fehler auswerten!!!
my $table = "spieler";
my $statement = "SELECT name, passwd from $table";
unless ($dbh->do($statement)) {
main::writeDbhError($dbh, "Error in executing Sql-Statement:\n\t$statement");
}
my $hash_ref = $dbh->fetchrow_hashref;
$dbh->disconnect();
Quote[Tue Oct 21 19:58:43 2003] login.cgi: Can't locate object method "fetchrow_hashref" via package "DBI::db" (perhaps you forgot to load "DBI::db"?) at login.cgi line 102.
[Tue Oct 21 19:58:43 2003] login.cgi: Database handle destroyed without explicit disconnect at login.cgi line 102.
1
2
3
4
5
6
7
my $table = "spieler";
my $statement = "SELECT name, passwd from $table";
my $sth = $dbh->prepare($statement) || die $dbh->errstr;
my $rv = $sth->execute;
my $hash_ref = $sth->fetchrow_hashref;
print "STH:" . Dumper( $sth );
1
2
3
4
5
6
7
8
if(my $hash_ref = $sth->fetchrow_hashref)
{
# gibt ergebnisse
}
else
{
# sorry, keine ergebnisse
}
1
2
3
4
5
6
while (my $data = $sth->fetchrow_hashref()) {
# mach was mit $data, z.B.
foreach my $field (keys %$data) {
print "$field => $data->{$field}\n";
}
} # while
1
2
3
my $arrayRef = $dbh->selectrow_array($statement) or die "Fehler: " . $dbh->errstr;
# oder:
my $hashRef = $dbh->selectrow_hashref($statement) or die "Fehler: " . $dbh->errstr;
5 Einträge, 1 Seite |