
FishMonger
Veteran
Feb 27, 2013, 11:47 AM
Views: 680
|
|
Re: [brosskgm] Perl MySQL help with code.
|
|
|
There's no need/reason to compare $id and $subscr_id. Instead, you should test $username. I'd also put the db code into a subroutine.
# retrieve user from the db my $username = get_user($id); if ( $username ) { .... .... .... } sub get_user { my $id = shift; my $host = "remotehost"; my $database = "dbase"; my $tablename = "tname"; my $user = "user"; my $pw = "password"; my $dbh = DBI->connect("DBI:mysql:$database:$host" , $user, $pw, { RaiseError => 1} ) or die "Connection Error: $DBI::errstr\n"; # no need to select subscr_id since we already know that value # and will be using it to retrieve the "username", if the id is found my $sql = "SELECT username FROM members WHERE subscr_id = ?"; my $sth = $dbh->prepare($sql); $sth->execute($id); my $username = $sth->fetchrow_array; $sth->finish(); $dbh->disconnect; return $username; # returns the 'username' or undef if not found in the db } Since you've made a number of changes to the script, we'll need to see your current version so we can point out other changes that it might need.
(This post was edited by FishMonger on Feb 27, 2013, 11:48 AM)
|