xref: /PHP-8.1/ext/pdo_dblib/tests/types.phpt (revision 74859783)
1--TEST--
2PDO_DBLIB: Column data types, with or without stringifying
3--EXTENSIONS--
4pdo_dblib
5--SKIPIF--
6<?php
7require __DIR__ . '/config.inc';
8?>
9--FILE--
10<?php
11require __DIR__ . '/config.inc';
12
13function get_expected_float_string() {
14    global $db;
15
16    switch ($db->getAttribute(PDO::DBLIB_ATTR_TDS_VERSION)) {
17        case '5.0':
18        case '6.0':
19        case '7.0':
20        case '7.1':
21        case '7.2':
22        case '8.0':
23            return '10.500';
24        default:
25            return '10.5';
26    }
27}
28
29$sql = "
30    SELECT
31        'foo' AS [char],
32        CAST('2030-01-01 23:59:59' AS DATETIME) AS [datetime],
33        CAST(0 AS BIT) AS [false],
34        10.500 AS [float],
35        1000 AS [int],
36        CAST(10.500 AS MONEY) AS [money],
37        CAST('1950-01-18 23:00:00' AS SMALLDATETIME) as [smalldatetime],
38        CAST(1 AS BIT) AS [true]
39";
40
41$stmt = $db->query($sql);
42$row = $stmt->fetch(PDO::FETCH_ASSOC);
43
44var_dump($row['char']);
45var_dump($row['datetime']);
46var_dump($row['false']);
47var_dump($row['float']);
48var_dump($row['int']);
49var_dump($row['money']);
50var_dump($row['smalldatetime']);
51var_dump($row['true']);
52
53$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
54$stmt = $db->query($sql);
55$row = $stmt->fetch(PDO::FETCH_ASSOC);
56
57var_dump($row['char']);
58var_dump($row['datetime']);
59var_dump($row['false']);
60var_dump($row['float'] === get_expected_float_string());
61var_dump($row['int']);
62// var_dump($row['money']); -- the decimal precision varies and it's not clear why
63var_dump($row['smalldatetime']);
64var_dump($row['true']);
65
66?>
67--EXPECT--
68string(3) "foo"
69string(19) "2030-01-01 23:59:59"
70int(0)
71float(10.5)
72int(1000)
73float(10.5)
74string(19) "1950-01-18 23:00:00"
75int(1)
76string(3) "foo"
77string(19) "2030-01-01 23:59:59"
78string(1) "0"
79bool(true)
80string(4) "1000"
81string(19) "1950-01-18 23:00:00"
82string(1) "1"
83