简体   繁体   English

PHP fetchAll PDO :: FETCH_OBJ和json_encode返回无效的JSON

[英]PHP fetchAll PDO::FETCH_OBJ and json_encode returns invalid JSON

I try to fetch some data from my MySQL database by using PDO::FETCH_OBJ and json_encode but it doesn't work since I added an DATE -field in the MySQL table. 我尝试通过使用PDO::FETCH_OBJjson_encode从我的MySQL数据库中获取一些数据,但是由于我在MySQL表中添加了DATE字段,所以它不起作用。

This is what my code looks like: 这是我的代码如下所示:

$app->get('/api/teams', function(Request $request, Response $response) {
$sql = "SELECT * FROM teams";

try {
    $db = new db();

    $db = $db->connect();

    $stmt = $db->query($sql);
    $teams= $stmt->fetchAll(PDO::FETCH_OBJ);
    $db = null;
    echo json_encode($teams);

   } catch(PDOException $e) {
       echo '{"error": {"text": '.$e->getMessage().'}';
   }
});

Before I added the DATE -field, it worked perfectly fine and it was returning an array with a bunch of objects. 在添加DATE字段之前,它工作得非常好,并且返回了带有一堆对象的数组。 Now it returns no data at all. 现在,它根本不返回任何数据。 Even though, when I forexample do call /api/teams/25 - I receive the correct data, even with the date-data. 即使,例如当我打电话给/api/teams/25 ,即使日期数据,我也会收到正确的数据。 So it"only" fails when I want to receive all teams/data at once. 因此,当我想一次接收所有团队/数据时,“仅”失败。

Can someone tell me whats wrong and how to fix it? 有人可以告诉我什么地方出了问题以及如何解决?

I can also tell that I tried to do this: 我还可以告诉我我尝试这样做:

foreach($teams as $team){
    echo json_encode($team);
}

But this returned invalid JSON data, which was a bunch of Objects without comma separation 但这返回了无效的JSON数据,这是一堆没有逗号分隔的对象

EDIT 编辑

My DB SCHEMA looks like this, 我的DB SCHEMA看起来像这样,

id (int(11), primary_key, auto_increment)
team ( varchar(255) )
country ( varchar(255) )
league ( varchar(255) )
creation_day ( date )

** EDIT 2 ** **编辑2 **

The invalid JSON which the foreach -example returns, looks like this: foreach -example返回的无效JSON如下所示:

{"id":"27", "Bayern München", "country":"Germany", "league": "Bundesliga, "creation_day": "2016-10-14"} {"id":"28", "Borussia Dortmund", "country":"Germany", "league": "Bundesliga, "creation_day": "2016-10-14"}

..and so on ..等等

The problem is you're not emitting one JSON document, but several that are just smashed together. 问题是您没有发出一个JSON文档,而是散发出了几个JSON文档。 That's not valid. 那是无效的。 Just do this: 只要这样做:

echo json_encode($teams);

PDO::FETCH_OBJ: returns an anonymous object with property names that correspond to the column names returned in your result set PDO :: FETCH_OBJ:返回一个匿名对象,该对象的属性名称与结果集中返回的列名称相对应

http://php.net/manual/en/pdostatement.fetch.php http://php.net/manual/zh/pdostatement.fetch.php

I suspect your use of PDO::FETCH_OBJ is the source of your problem. 我怀疑您对PDO::FETCH_OBJ是问题的根源。 I would suggest you change that to PDO::FETCH_ASSOC if you're just looking to output the entire result as JSON. 如果您只想将整个结果输出为JSON,建议您将其更改为PDO::FETCH_ASSOC

So your code example would look like: 因此,您的代码示例如下所示:

$app->get('/api/teams', function(Request $request, Response $response) {
$sql = "SELECT * FROM teams";

try {
    $db = new db();

    $db = $db->connect();

    $stmt = $db->query($sql);
    $teams= $stmt->fetchAll(PDO::FETCH_ASSOC); // <-- This changes
    $db = null;
    echo json_encode($teams);

   } catch(PDOException $e) {
       echo '{"error": {"text": '.$e->getMessage().'}';
   }
});

The reason I believe this is your issue is because JSON requires a certain format so if you were not getting any output from your echo then chances are your JSON was invalid. 我认为这是您的问题,原因是JSON需要某种格式,因此,如果您未从echo获取任何输出,则可能是JSON无效。 If you want to test you can check what you receive back from json_encode : 如果要测试,可以检查从json_encode返回的内容:

json_encode json_encode

Returns a JSON encoded string on success or FALSE on failure. 如果成功,则返回JSON编码的字符串;如果失败,则返回FALSE。

http://php.net/json_encode http://php.net/json_encode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM