1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
package Kernel::DB; use strict; use warnings; use DBI; sub new { my ( $Type, %Param ) = @_; my $Self = {}; bless( $Self, $Type ); $Self->{DBUser} = 'xxx'; $Self->{DBPassword} = 'xxx'; $Self->{DBName} = 'xxx'; $Self->{DBConnectString} = 'DBI:mysql:database=' . $Self->{DBName}; $Self->{dbh} = DBI->connect($Self->{DBConnectString}, $Self->{DBUser}, $Self->{DBPassword}); return $Self; } sub prepare { my ( $Self, %Param ) = @_; my $Querystring = $Param{Query}; $Self->{query} = $Self->{dbh}->prepare($Querystring); } sub fetchrow_array { my ( $Self, %Param ) = @_; $Self->{query}->execute or die $Self->{query}->err_str; return $Self->{query}->fetchrow_array(); } sub do { my ( $Self, %Param ) = @_; my $Querystring = $Param{Query}; $Self->{dbh}->do($Querystring); } 1;
1 2 3 4 5 6 7 8
my $DbObj = new Kernel::DB; $DbObj->prepare(Query => 'select * from user where lobbyname = "' . $Lobbyname . '"'); while(my @Rows = $DbObj->fetchrowarray()) { push @Userlist, $Rows[0]; }
1 2 3 4 5 6 7 8 9 10 11 12
my $dbh = DBI->connect('DBI:mysql:xxxx', 'xxxxx', 'xxx') || die "ERROR: $DBI::errstr"; my $sth = $dbh->prepare('select * from user where name = "' . $username . '"'); $sth->execute; my $counter = 0; while(my @Rows = $sth->fetchrow_array()) { $counter++; }
2017-11-24T18:46:26 GwenDragonVielleicht ist dein Code nur schnell für uns hier gekürzt, aber für Andere, Anfänger und Unvorsichtige sind diese Beispiele etwas riskant.
(…)