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

So I have this code. Essentially what it's supposed to do is, if there are values inside the cookies, it checks them against the users table. If it checks out, it gets the userid, and then figures out if they are the admin user. (admin user has a userid value of 1) After doing this, it replaces various commented sections in the template.html file, before printing it out again. However, I have the cookie set so that the admin user is logged in, and it's STILL telling me that "SELECT * FROM users failed.." in my template file. Does anyone have any idea exactly what's going wrong? When I comment out the first conditional, and the bottom else {} bit, it keeps telling me that $userid is equal to nothing. Help would be much appreciated.
Thanks,
Spidy
(Code Below)

##there ARE values in the cookies; check them against the database $uname_q = $dbh->quote($uname_c); $pword_q = crypt($pword_c,682); $pword_q = $dbh->quote($pword_q); if($dbh->selectrow_array("SELECT * FROM users WHERE name = $uname_ +q AND pass = $pword_q")){ ($userid) = $dbh->selectrow_array("SELECT userid FROM users WH +ERE name = $uname_q AND pass=$pword_q"); if($userid == 1) { ##admin user $template =~ s/<!--Mod (.+?)-->/$1/sg; $template =~ s/<!--Reg (.+?)-->//sg; $template =~ s/<!-- (.+?)-->//sg; } else { $template .= "\$userid = $userid"; $template =~ s/<!--Mod (.+?)-->//sg; $template =~ s/<!--Reg (.+?)-->//sg; $template =~ s/<!-- (.+?)-->//sg; } replace("loginout","Log Out"); replace("loginoutlink","logout"); $welmessage = "Welcome, $uname_c"; replace("welcome",$welmessage); $cookieexist = 1; } else { $template .= "SELECT * FROM users failed.."; replace("loginout","Log In"); replace("loginoutlink","login"); $template =~ s/<!--Mod (.+?)-->//sg; $welmessage = "You are not currently logged in."; $template =~ s/<!--Reg (.+?)-->/$1/sg; $template =~ s/<!-- (.+?)-->//sg; replace("welcome",$welmessage); }

Replies are listed 'Best First'.
Re: MySQL does not like me today...
by pg (Canon) on Sep 13, 2005 at 02:37 UTC

    Try to use place holder, it probably will help, at least remove one possible gotcha.

    SELECT * FROM users WHERE name = ? AND pass = ?

    Use the following syntax:

    @row_ary = $dbh->selectrow_array("SELECT * FROM users WHERE name = ? A +ND pass = ?", undef, @binding_values);

    Use $dbh->err() to tell you the exact database error.

    if you want to see whether there is a row satify certain condition, just say,

    SELECT 1 FROM users WHERE name = ? AND pass = ? #replace * with 1

    In your case, there is really no need to run the same query twice:

    if($dbh->selectrow_array("SELECT * FROM users WHERE name = $uname_q +AND pass = $pword_q")){ ($userid) = $dbh->selectrow_array("SELECT userid FROM users WH +ERE name = $uname_q AND pass=$pword_q");

    You said that after you comment out the first if and the else block, it complains about $userid. That is a good indication that your query probably failed, according to the document, an empty list will be retunred in that case.

Re: MySQL does not like me today...
by mrborisguy (Hermit) on Sep 13, 2005 at 05:18 UTC

    Somebody once told me that those suffixes at the end of variables actually are helpful, and they turned out to help me find this typo. (I think it's a typo, maybe it's what is supposed to happen.)

    $pword_q = $dbh->quote($pword_q);

        -Bryan