perl 执行mysql select 返回多条记录

[root@dr-mysql01 sbin]# cat t1.pl 
use DBI;
my $dbUser='DEVOPS';  
my $user="root";  
my $passwd="1234567"; 
$gp=$ARGV[0];
my @arr2="";  
my $dbh  = DBI->connect("dbi:mysql:database=DEVOPS;host=192.168.32.161;port=3306",$user,$passwd) or die "can't connect to database ". DBI-errstr;
my $hostSql = qq{select  IP,INFO,ENV from  machine_info  where ENV='$gp' }; 
my ($a1, $a2, $a3,$a4,$a5,$a6,$a7,$a8,$a9);  
my $selStmt = $dbh->prepare($hostSql);  
$selStmt->execute();  
$selStmt->bind_columns(undef, \$a1, \$a2, \$a3);  
#$selStmt->execute();  
while( $selStmt->fetch() ){ push (@arr2, "$a1  $a2  $a3\n" );
};
print "\@arr2 is @arr2\n";
You have mail in /var/spool/mail/root
[root@dr-mysql01 sbin]# perl t1.pl 
@arr2 is 
[root@dr-mysql01 sbin]# perl t1.pl 'env-zjtest5'
@arr2 is  192.168.32.22  haproxy  env-zjtest5
 192.168.32.24  front-end  env-zjtest5
 192.168.32.23  backoffice  env-zjtest5
 192.168.32.25  app  env-zjtest5
 192.168.32.26  zjzc-db  env-zjtest5
 192.168.32.27  pay-db  env-zjtest5
 192.168.32.28  redis  env-zjtest5
 192.168.32.29  nfs  env-zjtest5

posted @ 2016-05-03 10:45  czcb  阅读(403)  评论(0编辑  收藏  举报