• 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
    
    
  • 相关阅读:
    《那些年,我们拿下FPGA》做笔记
    三种初始化
    hdu4417 Super Mario 树阵离线/划分树
    【设计模式】文章摘要 查找联系人控件
    STL set
    阐述 QUEST CENTRAL FOR DB2 八罪
    使用线程执行堆栈StackTraceElement设计Android日志模块
    苹果iOS苹果公司的手机用户都有权索赔
    Android 4.4 沉浸式透明状态栏与导航栏
    常见的几种RuntimeException
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349834.html
Copyright © 2020-2023  润新知