newbie430 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to create a session for my web application using Dancer2. I currently using mysql database. I searched alot of reference on the internet because I'm new to Perl it seem the references are too complicated. Please guide me. Here is my .pm code.

post '/' => sub { my $username = $q->param("username"); my $password = $q->param("password"); my $loginstatement = 'SELECT * FROM account WHERE username=? a +nd password=?'; my $sth = $dbh->prepare($loginstatement) or die $dbh->errstr; $sth->execute(params->{'username'}, params->{'password'}) or d +ie $sth->errstr; my (@userID) = $sth->fetchrow_array; if ($userID[0] != 0 && $userID[11] ==1 ){ my $sessionstatement = "SELECT * FROM sessions"; my $sth2 = $dbh->prepare($sessionstatement) or die $db +h ->errstr; my (@sessID)= $sth2->fetchrow_array; if($userID[3] == $sessID[1]) { session 'logged_in' => true; }

Replies are listed 'Best First'.
Re: session with mysql database.
by Anonymous Monk on Apr 14, 2015 at 08:59 UTC

    Please guide me. Here is my .pm

    If that is your .pm, it is incomplete -- this is not the beginning

    Also, the code you posted features a suspiciously named variable $q ... I've seen others use CGI; inside Dancer ... backwards if thats what it is

    Also, you seem to be using "session" keyword/helper, but then you're also using $dbh to query a database ... weird thing to do ... normally you'd configure a Dancer2::Session::DBIC backend and then "session" keyword/helper just does that for you

Re: session with mysql database.
by locked_user sundialsvc4 (Abbot) on Apr 15, 2015 at 14:16 UTC

    “If you are using Dancer2, then use all of Dancer2.”   Session-management and lots of other things are already handled by available plugins which you can either use or slightly-adapt.   “Taking a nice stroll through CPAN” should be the very first thing that you do.   Indeed, that’s the point of using a good framework.   “Do not do a thing already done.™”