Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 15th, 2001, 01:47 PM
Stenyj Stenyj is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Posts: 3 Stenyj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question

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







Reply With Quote
  #2  
Old March 15th, 2001, 02:52 PM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Wink

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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > How do I have a script check and give a true/false value if data in SQL table.....


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT