CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: DBI: Re: [brosskgm] Perl MySQL help with code.: Edit Log



FishMonger
Veteran / Moderator

Feb 27, 2013, 11:47 AM


Views: 4254
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.


Code
# 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)


Edit Log:
Post edited by FishMonger (Veteran) on Feb 27, 2013, 11:48 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives