0
Connect with mysql and retrive data from the table.

  my $db ="JJusers";
  my $user ="root";
  my $password ="abcdef";
  my $host ="localhost";

  my $dbh =DBI->connect("DBI:mysql:$db:$host",$user,$password);

  my $uDt = $dbh->prepare("select Username,Password from Users");
  my $rv = $uDt->execute;

  print "<script>alert($rv)</script>";

When I execute this code I am getting the result as 1. In database the data stored as:

1, jj, pp(SNO, USERNAME,PASSWORD)

Why isn't it getting the right data?

1
  • Are you following the DBI documentation? You can get it either from perldoc DBI, or online at search.cpan.org/dist/DBI Commented Sep 5, 2013 at 12:34

2 Answers 2

4

You are printing the result of execute, not the actual database results. You want to do something like this...

while (my @data = $rv->fetchrow_array()) {
    my $username = $data[0];
    my $password = $data[1];
    // ...
}
Sign up to request clarification or add additional context in comments.

5 Comments

Global symbol "@data" requires explicit package name . how to fix this?
Use my before @data. It's because you are using use strict Answer updated.
How to redirect the and page when the button is clicked. how to work onclick in perl cgi script.
You should post that as a new question along with a code sample of what you tried.
print "User name:\t"; print $q->textfield( -name => 'UserName', -value =>'JJ', -size => 30, -maxlength =>50, ); my $uName = $q->param('UserName'); print "<script>alert($uName)</script>"; when i use this i dont get jj on the alert its not reading the textfield value. i want to the value of the text field
0

->execute returns just query result(0, 1, 0E0), but not resultset. As for me, best way is:

my $res = $dbh->selectall_arrayref('select Username,Password from Users', {Slice=>{}});
# now, you can iterate result.
# for example:
foreach my $row(@$res) {
    print $row->{Username};
}

If you need bind vaiables, you can use selectall_arrayref also:

my $res = $dbh->selectall_arrayref('select Username,Password from Users where id = ?',
    {Slice=>{}}, 1
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.