mysqldatabaseperldbihashref

Perl fetchrow_hashref results are different integer vs. string values


I really need your help for understanding with the following perl example code:

#!/usr/bin/perl

# Hashtest

use strict;
use DBI;
use DBIx::Log4perl;
use Data::Dumper;
use utf8;

if (my $dbh = DBIx::Log4perl->connect("DBI:mysql:myDB","myUser","myPassword",{
            RaiseError => 1,
            PrintError => 1,
            AutoCommit => 0,
            mysql_enable_utf8 => 1
        }))
{

    my $data = undef;
    my $sql_query = <<EndOfSQL;
SELECT  1
EndOfSQL
    my $out = $dbh->prepare($sql_query);
    $out->execute() or exit(0);
    my $row = $out->fetchrow_hashref();
    $out->finish();

    # Debugging
    print Dumper($row);

    $dbh->disconnect;
    exit(0);
}

1;

If i run this code on two machines i get different results.

Result on machine 1: (Result i needed with integer value)

arties@p51s:~$ perl hashTest.pl 
Log4perl: Seems like no initialization happened. Forgot to call init()?
$VAR1 = {
          '1' => 1
        };

Resulst on machine 2: (Result that makes trouble because of string value)

arties@core3:~$ perl hashTest.pl
Log4perl: Seems like no initialization happened. Forgot to call init()?
$VAR1 = {
          '1' => '1'
        };

As you can see on machine 1 the value from MySQL will be interpreted as integer value and on machine 2 as string value. I need on both machines the integer value. And it is not possible to modify the hash later, because the original code has too much values, that must be changed...

Both machines uses DBI 1.642 and DBIx::Log4perl 0.26

The only difference is the perl version machine 1 (v5.26.1) vs. machine 2 (v5.14.2)

So the big question is, how can I make sure I always get the integer in the hash as the result?

Update 10.10.2019:

To show perhaps better the problem, i improve the above example:

...
use Data::Dumper;
use JSON;  # <-- Inserted
use utf8;
...

...
print Dumper($row);

# JSON Output
print JSON::to_json($row)."\n"; # <-- Inserted

$dbh->disconnect;
...

Now the output on machine 1 with last line the JSON Output:

arties@p51s:~$ perl hashTest.pl 
Log4perl: Seems like no initialization happened. Forgot to call init()?
$VAR1 = {
          '1' => 1
        };
{"1":1}

Now the output on machine 2 with last line the JSON Output:

arties@core3:~$ perl hashTest.pl
$VAR1 = {
          '1' => '1'
        };
{"1":"1"}

You see, that both Data::Dumper AND JSON has the same behavor. And as i wrote bevor, +0 is not an option because the original hash is much more complex.

Both machines use JSON 4.02


Solution

  • @Nick P : That's the solution you linked Why does DBI implicitly change integers to strings? , the DBD::mysql was different on both systems! So i upgraded on machine 2 from Version 4.020 to Version 4.050 and now both systems has the same result! And Integers are Integers ;-)

    So the result on both machines is now:

    $VAR1 = {
              '1' => 1
            };
    {"1":1}
    

    Thank you!