为什么我的 Perl DBI 查询在 CGI 脚本中不返回任何结果?

发布于 2024-08-20 07:46:16 字数 1276 浏览 8 评论 0原文

我是第一次使用 DBI(刚开始使用 Perl [2 周]),我似乎无法从数据库中获得任何结果。这就是我所拥有的:

if( defined( $get{findAllPages} ) && defined( $post{ki} ) ){

   my ($database, $hostname, $port, $password, $user );

   $database = "#########";
   $hostname = "localhost";
   $password = "#########";
   $user = "###########";
   my $KI = $post{ki};

   # connect to the database
   my $dsn = "DBI:mysql:database=$database;host=$hostname;";
   my $dbh = DBI->connect($dsn, $user, $password);
   my $sth = $dbh->prepare("SELECT * FROM accounts WHERE KI = '" . $dbh->quote($KI) . "' ") or die "Could not select from table";
   $sth->execute();
   if( $sth->rows != 0 ) {
      my $ref = $sth->fetchrow_hashref();
      my $domain = $ref->{website};
      my $DB_username = $ref->{db_name};
      my $DB_password = $ref->{db_pass};
      $sth->finish();
      $dbh->disconnect();

      print "domian: " . $domain . "<br />\n";

      chomp(my $url = trim($domain));

就目前情况而言,它检查 KI 是否正确,然后检查有效的行数。我无法开始工作的是从数组返回值;

my $ref = $sth->fetchrow_hashref();
my $domain = $ref->{website};
my $DB_username = $ref->{db_name};
my $DB_password = $ref->{db_pass};

如果有人能让我知道我哪里出错了,我将不胜感激。

I'm using DBI for the first time (and not long started Perl [2 weeks]) and I can't seem to get any results from the database. Here's what I have:

if( defined( $get{findAllPages} ) && defined( $post{ki} ) ){

   my ($database, $hostname, $port, $password, $user );

   $database = "#########";
   $hostname = "localhost";
   $password = "#########";
   $user = "###########";
   my $KI = $post{ki};

   # connect to the database
   my $dsn = "DBI:mysql:database=$database;host=$hostname;";
   my $dbh = DBI->connect($dsn, $user, $password);
   my $sth = $dbh->prepare("SELECT * FROM accounts WHERE KI = '" . $dbh->quote($KI) . "' ") or die "Could not select from table";
   $sth->execute();
   if( $sth->rows != 0 ) {
      my $ref = $sth->fetchrow_hashref();
      my $domain = $ref->{website};
      my $DB_username = $ref->{db_name};
      my $DB_password = $ref->{db_pass};
      $sth->finish();
      $dbh->disconnect();

      print "domian: " . $domain . "<br />\n";

      chomp(my $url = trim($domain));

As it stands it checks to see if KI is correct and then checks row amount which works. The bit I can't get to work is returning values from the array;

my $ref = $sth->fetchrow_hashref();
my $domain = $ref->{website};
my $DB_username = $ref->{db_name};
my $DB_password = $ref->{db_pass};

If anyone can let me know where I am going wrong it would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆光飞翔i 2024-08-27 07:46:16

尝试使用 DBI 的错误处理来查看问题所在。请参阅编程 Perl DBI,第 4 章(错误处理)或 Perlmonks 上的“DBI::mysql 错误处理” 供参考。

Try to use DBI's error handling to see what's wrong. See Programming the Perl DBI, chapter 4 (Error handling) or "DBI::mysql error handling" on Perlmonks for references.

離人涙 2024-08-27 07:46:16

当我看不到发生了什么时,我经常做的事情是:

使用 Data::Dumper;

我的 $ref = $sth->fetch();

打印转储器$ref;

您将看到数据结构布局。有时,这是一个数组或散列索引不正确的问题。

What I often do when I can't see what is going on:

use Data::Dumper;

my $ref = $sth->fetch();

print Dumper $ref;

and you'll see the data structure layout. Sometimes it is a matter of incorrect indexing into an array or hash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文