How do I get the PDO to list the Array with the Correct Types?

0

In MySQL I write data in a table defining some fields as int or float , but when I make a select in the database via PDO the array comes all as a string. Is there a function that forces the type to be returned as it is in the database?

See:

 array(13) {
  ["id"]=>
  string(4) "1713"
  ["id_shop_default"]=>
  string(1) "5"
  ["id_envio_default"]=>
  string(3) "200"
  ["regiao"]=>
  string(19) "SAO PAULO - CAPITAL"
  ["cep_inicio"]=>
  string(9) "01000-001"
  ["cep_fim"]=>
  string(9) "05999-999"
  ["peso_inicial"]=>
  string(5) "0.000"
  ["peso_final"]=>
  string(5) "1.000"
  ["valor"]=>
  string(5) "22.70"
  ["prazo_entrega"]=>
  string(1) "7"
  ["ad_valorem"]=>
  string(4) "1.00"
  ["kg_adicional"]=>
  string(4) "1.72"
  ["created"]=>
  string(19) "2016-09-19 17:11:09"
}

The same value is float !

Thank you in advance.

Edit:

<?php    

try{    

  $opt = array(PDO::ATTR_STRINGIFY_FETCHES => false);

  // Faz conexão com banco de daddos
  $pdo = new PDO("mysql:host=127.0.0.1;dbname=banco","root", "123456", $opt);


}catch(PDOException $e){
  // Caso ocorra algum erro na conexão com o banco, exibe a mensagem
  echo 'Falha ao conectar no banco de dados: '.$e->getMessage();
  die;
}

try {

  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    

  $consulta = $pdo->query("SELECT * FROM transportadora");
  $linha = $consulta->fetch(PDO::FETCH_ASSOC);    

  echo "<pre>";
  var_dump($linha);
  echo "</pre>";


} catch (PDOException $e) {
    echo $e->getMessage();
}
    
asked by anonymous 19.09.2016 / 22:45

1 answer

1

Make sure PDO::ATTR_STRINGIFY_FETCHES is marked as false This option can be applied via setAttribute() or direct in the PDO builder.

$opt = array(PDO::ATTR_STRINGIFY_FETCHES => false);
$pdo = new PDO('mysql:host=localhost;dbname=base', 'usuario', 'senha', $opt);
    
19.09.2016 / 22:53