in reply to Re: accessing values in hash
in thread accessing values in hash

hi there, there is no problem accessing the database. I am using;
$sth = $dbh->prepare("select username, password from reg"); $sth->execute;
and then the previous code I have posted to fetch and display it. But I need to print out whether the username i have it $testUsername matches one of the usernames returned from the database

Replies are listed 'Best First'.
Re^3: accessing values in hash
by swampyankee (Parson) on Sep 03, 2006 at 23:40 UTC

    Sorry, I posted before you posted your answer.

    emc

    Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.

    Albert Einstein
      sorry I have never came across grep. You mean I should be looking to do something like:
      $sth = $dbh->prepare(q{select username, password from reg where username = ?}); $sth->execute($testUsername);

        grep applies a test to all the elements of a list and creates a subset of the elements that pass the test. Using it in scalar context return the number of elements which passed the test.

        However, if you know the username first, I would suggest making the database do the work and re-writing the query to something like this (note that my SQL skills are quite rusty):

        $sth = $dbh -> prepare(q{select username, password from reg where user +name = $testUsername});

        Why return a list to Perl and check the entire list for the existence of username when the database can do exactly that? If you're testing a large number of user names, it may make more sense to query the database for a list, and then check the list for the for existence of all the user names(subject to the caution that the database may have been updated during the processing of the list).

        emc

        Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.

        Albert Einstein