如何从 Perl 的 DBI 获取 MySQL 查询的结果?

发布于 2024-09-18 13:21:38 字数 432 浏览 7 评论 0原文

我正在执行以下操作,并得到“1”,我认为这意味着该语句进展顺利。但我想要结果。

怎么了?

#!/usr/bin/perl

use strict;
use DBI;

my $host = "test";
my $database = "dd";
my $port = 3306;
my $user = "uuu";
my $pw = "ppp";

my $mysql = DBI->connect("DBI:mysql:database=$database;host=$host;port=$port", $user, $pw)
    or die "Cannot connect to MySQL server\n";

my $m = $mysql->do(qq{select MAX(idvisit) from log_visit});

print $m;

I am doing the following, and getting "1" which I assume means the statement wend well. But I would like the result instead.

What's wrong?

#!/usr/bin/perl

use strict;
use DBI;

my $host = "test";
my $database = "dd";
my $port = 3306;
my $user = "uuu";
my $pw = "ppp";

my $mysql = DBI->connect("DBI:mysql:database=$database;host=$host;port=$port", $user, $pw)
    or die "Cannot connect to MySQL server\n";

my $m = $mysql->do(qq{select MAX(idvisit) from log_visit});

print $m;

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

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

发布评论

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

评论(4

久而酒知 2024-09-25 13:21:38
my $m = $mysql->prepare("select MAX(idvisit) from log_visit");
$m->execute() or die "Couldn't execute statement: ".$m->errstr;
print $m->fetch()->[0];
my $m = $mysql->prepare("select MAX(idvisit) from log_visit");
$m->execute() or die "Couldn't execute statement: ".$m->errstr;
print $m->fetch()->[0];
平定天下 2024-09-25 13:21:38

检查文档以了解您遇到问题的功能总是值得的。

在这种情况下,“do”的 DBI 文档 说:

准备并执行单个
陈述。返回行数
错误时受影响或未定义。

而且,更明确地说,

它不应该用于 SELECT
语句,因为它不返回
一个语句句柄(所以你无法获取
任何数据)。

It's always worth checking the documentation for functions that you're having trouble with.

In this case the DBI documentation for "do" says:

Prepare and execute a single
statement. Returns the number of rows
affected or undef on error.

And, more explicitly,

It should not be used for SELECT
statements because it does not return
a statement handle (so you can't fetch
any data).

樱花坊 2024-09-25 13:21:38

do 返回受影响的行数。您可能想查看statement 类,特别是execute 函数。

do returns the number of affected rows. You might want to look into the statement class and specifically, the execute function.

帥小哥 2024-09-25 13:21:38
my $m = $mysql->selectrow_array(qq{select MAX(idvisit) from log_visit});
my $m = $mysql->selectrow_array(qq{select MAX(idvisit) from log_visit});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文