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>";

当我执行此代码时,我得到的结果为1. 在数据库中,数据存储为:

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

为什么它没有得到正确的数据?

4

2 回答 2

4

您正在打印 的结果execute,而不是实际的数据库结果。你想做这样的事情......

while (my @data = $rv->fetchrow_array()) {
    my $username = $data[0];
    my $password = $data[1];
    // ...
}
于 2013-09-05T12:28:59.920 回答
0

->execute 只返回查询结果(0, 1, 0E0),但不返回结果集。至于我,最好的方法是:

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};
}

如果你需要绑定变量,你也可以使用 selectall_arrayref:

my $res = $dbh->selectall_arrayref('select Username,Password from Users where id = ?',
    {Slice=>{}}, 1
);
于 2013-09-06T07:42:36.460 回答