gpt4 book ai didi

mysql - 使用perl连接到数据库时打印内容

转载 作者:行者123 更新时间:2023-11-29 08:42:55 25 4
gpt4 key购买 nike

下面是使用perl连接mysql数据库并检索结果的代码。

在下面的示例中,样本表有 10 列。我只想将记录 99 的第二列和第三列放入变量中。

有人可以帮我吗?

use strict;
use warnings;
use DBI;

my $dbh = DBI->connect('dbi:mysql:perltest','root','password') or die "Connection Error: $DBI::errstr\n";
my $sql = "select * from samples where record='99'";
my $sth = $dbh->prepare($sql);
$sth->execute or die "SQL Error: $DBI::errstr\n";
while (my @row = $sth->fetchrow_array) {
print "@row\n";
}

提前致谢

最佳答案

此代码会将行的第二列和第三列放入变量中:

while (my @row = $sth->fetchrow_array) {
$var1 = @row[1];
$var2 = @row[2];
}

关于mysql - 使用perl连接到数据库时打印内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13260881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com