zoukankan      html  css  js  c++  java
  • perl 解决mysql utf8中文乱码 问题

    mysql utf8 中文问题:
    zabbix:/root/sbin# cat a1.pl 
    use DBI;
    my $dbUser='DEVOPS';
    my $user="root";
    my $passwd="kjk123123";
    my $dbh  = DBI->connect("dbi:mysql:database=DEVOPS;host=192.168.11.185;port=3306",$user,$passwd) or die "can't connect to database ". DBI-errstr;
    ##防止utf-8中文乱码
    @arr2=();
    #$dbh->do("SET NAMES utf8");
    my $hostSql = qq{select  IP,INFO,ENV from  machine_info where env='esx-192.168.4.41'};
    my ($a1, $a2, $a3,$a4,$a5,$a6,$a7,$a8,$a9);
    my $selStmt = $dbh->prepare($hostSql);
    $selStmt->execute();
    $selStmt->bind_columns(undef, $a1, $a2, $a3);
    print "$a1,$a2,$a3
    ";
    print "11111111111111111
    ";
     while( $selStmt->fetch() )
     { push (@arr2, "$a1  $a2  $a3
    " );
     };
    print @arr2;
    
    
    此时乱码
    
    zabbix:/root/sbin# perl a1.pl 
    ,,
    11111111111111111
    192.168.11.134  BI?????????  esx-19.16.4.41
    
    
    
    开启utf8:
    $dbh->do("SET NAMES utf8");
    zabbix:/root/sbin# perl a1.pl 
    
    
    
    或者:
    
    zabbix:/root/sbin# cat a1.pl 
    use DBI;
    use Encode;
    my $dbUser='DEVOPS';
    my $user="root";
    my $passwd="kjk123123";
    my $dbh  = DBI->connect("dbi:mysql:database=DEVOPS;host=192.168.11.185;port=3306",$user,$passwd,{mysql_enable_utf8 => 1}) or die "can't connect to database ". DBI-errstr;
    ##防止utf-8中文乱码
    @arr2=();
    #$dbh->do("SET NAMES utf8");
    my $hostSql = qq{select  IP,INFO,ENV from  machine_info where env='esx-192.168.4.41'};
    my ($a1, $a2, $a3,$a4,$a5,$a6,$a7,$a8,$a9);
    my $selStmt = $dbh->prepare($hostSql);
    $selStmt->execute();
    $selStmt->bind_columns(undef, $a1, $a2, $a3);
    print "$a1,$a2,$a3
    ";
    print "11111111111111111
    ";
     while( $selStmt->fetch() )
     {$a2=encode_utf8($a2); push (@arr2, "$a1  $a2  $a3
    " );
     };
    print @arr2;
    zabbix:/root/sbin# perl a1.pl 
    ,,
    11111111111111111
    
    
  • 相关阅读:
    Windows 2003,XP安装Windows Phone 7 狼人:
    Android实例剖析笔记(六) 狼人:
    Android实例剖析笔记(一) 狼人:
    Android NDK带来什么 狼人:
    Windows Phone 7常用资源大集合 狼人:
    Android实例剖析笔记(七) 狼人:
    JDK环境变量配置
    jQuery去掉字符串起始和结尾的空格
    结构型设计模式
    主题:hibernate生成tree(基于注解方式)
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349834.html
Copyright © 2011-2022 走看看