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.


In reply to Re: perl grep help! by jrsimmon
in thread perl grep help! by bogglemaster89

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.