PHP PDO - 为什么我的fetchAll()在使用json_encode时将所有值转换为字符串?

时间:2018-03-25 16:10:07

标签: php

我在表上选择所有SQL查询并在PHP中准备好的查询中运行它。然后我在json_encode中回显结果。结果是将每个值都作为字符串,甚至是作为INT的行ID。你如何保留原始价值类型?

    $sql = "SELECT * FROM `Type`";
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode($result);

输出如下:

[{"ID":"1","Type":"Classic Starters","Description":""},{"ID":"2","Type":"Special Starters","Description":""}]

所需的输出如下:

[{"ID":1,"Type":"Classic Starters","Description":""},{"ID":2,"Type":"Special Starters","Description":""}]

提前致谢< 3

1 个答案:

答案 0 :(得分:0)

char *PTR = '\0';
char CHAR = '\0';

char *PTR = NULL;
char CHAR = NULL;  ///< will generate a warning

这解决了我的问题