Find the answer to your Linux question:
Results 1 to 2 of 2
I want to do following in PerCGI:- i) Want to validate "username" is already exists in DataBase or not? ii) Can I check whether $sth contains some values or not ...
  1. #1
    Linux Newbie
    Join Date
    Jan 2008
    Posts
    114

    how to validate username with DataBase in PerlCGI

    I want to do following in PerCGI:-

    i) Want to validate "username" is already exists in DataBase or not?
    ii) Can I check whether $sth contains some values or not from below mentioned statement?
    iii) If $sth is not containing any value, means $user is not exists in Database then what'll fetchrow_array return.

    $sql = Select * from userinfo where username=$user;
    my $sth = $dbh->prepare($sql);
    $sth->execute;
    my @row = $sth->fetchrow_array

    Please help me

    Thanks

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    First off, remember to quote your assignment to $sql.

    Secondly, so you want to know the number of rows that some query returns. This can be done by changing your SQL query and doing the following:
    Code:
    my $sql = "SELECT COUNT(*) FROM userinfo WHERE username = ?";
    my $sth = $dbh->prepare($sql);
    $sth->execute($user);
    my $count = $sth->rows;
    $count now contains the number of rows that have a username of $user.

    Alternatively, as per the CPAN documentation of the DBI module:
    Quote Originally Posted by DBI Documentation
    If there are no more rows or if an error occurs, then fetchrow_array returns an empty list.
    You might prefer fetchrow_arrayref, because:
    Quote Originally Posted by DBI Documentation
    If there are no more rows or if an error occurs, then fetchrow_arrayref returns an undef.
    And an undef is simple to check for.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...