0

When I select a column in SQL Server and then print it, 3 becomes 3.0.

This is my connection:

$sql = new PDO("sqlsrv:Driver=$driver;server=$server,$port;Database=$database;ConnectionPooling=0", $uid, $pwd,
        array(
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        )
    );

This is my SELECT:

$sth = $sql->prepare("SELECT column FROM table
                      WHERE column2 = :value2");
    
$params = array(':value2' => $_POST['value2']);
    
$sth->execute($params);
$result = $sth->fetchAll(\PDO::FETCH_ASSOC);
   
echo json_encode($result);
  • In SQL Server database: 3
  • In echo: 3.0

What can be the reason for adding .0?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Björn C
  • 3,860
  • 10
  • 46
  • 85
  • This might help you: https://stackoverflow.com/a/6608413/673707 – Helio Sep 07 '22 at 09:19
  • @Zhorov I'm having problems with intergrating "This" into my select. I have no second value like: $sth->bindColumn('Antal', ?, PDO::PARAM_INT); And the second "...NUMERIC_TYPE" did no change. – Björn C Sep 07 '22 at 09:42
  • @Zhorov PHP 8.0 driver 5.10 – Björn C Sep 08 '22 at 07:06
  • @Zhorov In MSSQL it's float – Björn C Sep 08 '22 at 07:07
  • @BjörnC, did you try `$conn->setAttribute(PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE , true);` to check the difference? – Zhorov Sep 08 '22 at 07:52
  • @BjörnC, a hint: `$stmt = $conn->prepare($sql); $stmt->execute(); $result = array(); $stmt->bindColumn('column', $column, PDO::PARAM_INT); while ($row = $stmt->fetch(PDO::FETCH_BOUND) ){$result[]["column"] = $column;}; echo json_encode($result);`. – Zhorov Sep 08 '22 at 08:04

0 Answers0