Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This wont work ugh?!?!?!
while ( $users = $sth->fetchrow_array ) { if(param("username") =~ /$users/i) { error("Username already exists, please make a different one"); } }

Replies are listed 'Best First'.
Re: /i regex modifier
by japhy (Canon) on Jul 18, 2002 at 19:31 UTC
    This is so the wrong approach to take. Why are you going through all a list of database results searching for a username? ASK THE DATABASE!
    my $sql = q{SELECT username FROM db WHERE username=?}; my $sth = $dbh->prepare($sql); $sth->execute(param("username"));
    Now, if there's a result, the username existed in the database. And if you want to compare in lowercase, there are SQL utilities to do that as well. Don't use a regex. You're going about this the wrong way.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      sorry Im trying to say, if user is eq to one of the usernames in the dtabase then give a error, but it prints out this error if I do that.
      Quantifier follows nothing before HERE mark in regex m/? << HERE / at +/home/dtdynoco/public_html/admin1130/register.cgi line 30.
        You do NOT want to use a regex. A regex does not test for equality. If you want EQuality, use eq. You even used "eq" as a word in your node!

        Apparently, $users starts with a "?". But this is pointless. Don't use a regex. Do not.

        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: /i regex modifier
by Corion (Patriarch) on Jul 18, 2002 at 19:18 UTC

    In what sense won't it work ?
    What does @$users contain ?
    What do you want to do ?
    Please read How (Not) To Ask A Question to give us the chance to help you.

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
      sorry Im trying to say, if user is eq to one of the usernames in the dtabase then give a error, but it prints out this error if I do that.

      Quantifier follows nothing before HERE mark in regex m/? << HERE / at /home/dtdynoco/public_html/admin1130/register.cgi line 30.
Re: /i regex modifier
by dws (Chancellor) on Jul 18, 2002 at 19:21 UTC
    while ( $users = $sth->fetchrow_array ) { if(param("username") =~ /$users/i) {
    The problem you're running into is that $users is going to be a reference, not a scalar string value.

    Assuming that the user is the first element in the array you just fetched, try   if ( param("username") =~ /$$users[0]/i ) { Bah. I was getting fetchrow_array() confused with fetchrow_arrayref() (Thanks, jwest).

    So, add a print statement. What is in $users? What is in param("username")?

    Regardless, take japhy's advice, and don't do this with a regex.