Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: perl grep help!

by jrsimmon (Hermit)
on Mar 09, 2010 at 17:09 UTC ( [id://827588]=note: print w/replies, xml ) Need Help??


in reply to perl grep help!

Let's ignore for a moment the strange use of "select *" when what you really want is two specific columns and the seemingly unnecessary usage of grep...

In that world, you could do something along the lines of:

#!C:\strawberry\perl\bin\perl.exe use dbi; use strict; use warnings; my $usernameentered = "user89"; my $passwordentered = "password12"; my $dbh = DBI->connect('dbi:mysql:Database','username','password') or die "Connection Error: $DBI::errstr\n"; my $sql = "select * from usertable"; my $sth = $dbh->prepare($sql); $sth->execute or die "SQL Error: $DBI::errstr\n"; while (my @row = $sth->fetchrow_array) { my @username= grep /$usernameentered/, @row; print "@username\n"; my @password= grep /$passwordentered/, @row; print "@password\n"; #if (@username !=$usernameentered) if ($username[0] ne $usernameentered)#??which index?? { print "incorrect username entered **$username[0]**\n"; } #if (@password !=$passwordentered) if ($password[0] ne $passwordentered)#??which index?? { print "incorrect password entered $password[0]**\n"; } }

Now, back in the real world, why not just let the database do the work for you?

#!C:\strawberry\perl\bin\perl.exe use dbi; use strict; use warnings; my $usernameentered = "user89"; my $passwordentered = "password12"; my $dbh = DBI->connect('dbi:mysql:Database','username','password') or die "Connection Error: $DBI::errstr\n"; my $sql = "select user from usertable where user = ? and pwd = ?"; my $sth = $dbh->prepare($sql) or die "SQL Error: $DBI::errstr\n"; $sth->execute($usernameentered, $passwordentered) or die "SQL Error: $DBI::errstr\n"; while (my @row = $sth->fetchrow_array) { #do whatever you want with this user }

The last thing that jumps out is that you are evidently storing pwds in the database in clear text. That's a no-no, unless you really don't care about the passwords, in which case why use them in the first place? But, perhaps this is not your decision. If it is, you should use one of the many one-way hash modules on cpan to create a hash of the password and then store THAT, rather than storing the password itself.

Replies are listed 'Best First'.
Re^2: perl grep help!
by bogglemaster89 (Novice) on Mar 09, 2010 at 17:36 UTC
    hi with the 'let the database work for you' example....how would I get it so users define the username and password as its meant to be a login type script? also im ignoring the login issues i.e. access to passwords till the end. Thanks for your help so far! bogglemaster89
      How would I get it so users define the username and password as its meant to be a login type script?

      You ask for user input, whether reading from the STDIN, taking command-line arguments, or reading a file with login credentials.

        i added:
        print "please enter a username /n"; my $usernameentered = <STDIN>; print "please enter a password /n"; my $passwordentered = <STDIN>;
        to the top of the script i also changed the ? to a real username and password it failed with the following.... DBD::mysql::st execute failed: called with 2 bind variables login.pl line 16. SQL Error: called with 2 bind variables when 0 are needed im guessing this maybe becasue this is where my <STDIN> where meant to go? sorry to keep bombarding you with questions but im really stuck! bogglemaster89 UPDATE- I replaced what I typed with question marks, it runs now, but im not sure what to do to get it to match these passwords and if they dont match through an error.......to the end-user of course not me!
      You'll have to request their input, presumably via STDIN. You'll also want to look at Term::ReadKey so that their passwords aren't printed to the screen when typed.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://827588]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2025-02-13 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found