|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi,
I'm trying to setup a type of login script. User information will be stored in a myL database. When the person logs in (enters their login and PW) I want the script to be able to check to see if both the login and PW entered exist, and on the same "record" in the table in mySQL. If it does, I want it to return a "true" value, if not a "false" value. I wan to do something similar to the following, but the script just stops if no record exists with the info entered: ------------------------------- #!/usr/bin/perl use CGI; use DBI; $foo = new CGI; $database = "*database name*"; $dbuser = "*my user name*"; $dbpassword = "*my password*"; $dbh = DBI->connect("DBI:mysql:$database", $dbuser, $dbpassword); print $foo->header; print "<HTML>\n"; print "<BODY BGCOLOR=WHITE TEXT=BLACK>\n"; $login = $foo->param('login'); $pw = $foo->param('password'); $statement = "SELECT user_access FROM table_name WHERE login = '$login' AND password = '$pw'"; $sth = $dbh->prepare($statement) or die "Can't prepare $statement: $dbh->errstr\n"; $sth->execute or die "Can't execute the query: $sth->errstr"; @row = $sth->fetchrow_array; $user_access = $row[0]; $sth->finish; # this is where I'd like to do an "if" statement, to see if # that exists. IF the account exists, it will have pulled # out an access level for that account. IF it doesn't # exist, the script just stops responding because I guess # it gets an error when trying to look for something that # doesn't exist. if ($user_access) { blah blah blah } else { blah blah blah } etc. ------------------------------------- Anyone have any suggestions? Thoughts? Any feedback is appreciated. Stenyj |
|
#2
|
|||
|
|||
|
Ok, well, the query should only be returning one row if you are writing it right which it looks like you are. However you're query is written wrong for PerlDBI. YOu have to use placeholder"?" in your query to be filled later by the arguments passed to execute(I'll address that below).
One solution is the rows function in DBI. This function returns the number of rows affected by your last query: If it's 1 row, the info is good. If its anything else it's bad. Code:
$sth->prepare( "SELECT user_access FROM table_name WHERE login = ? AND password = ?" ); # NOTE the ? placeholders
$sth->execute( $login, $pass ); # the args to execute are the vars you want plugged in
$rows = $sth->rows();
if ( $rows == 1 ) {
# everything is good
# process the info
# here you'd actually bind the info with DBI functions
} else {
# bad input
# rerun the original login code
}
__________________
- dsb - ![]() Perl Guy |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > How do I have a script check and give a true/false value if data in SQL table..... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|