代码之家  ›  专栏  ›  技术社区  ›  Ricard Espinàs Llovet

symfony条令结果查询格式错误

  •  0
  • Ricard Espinàs Llovet  · 技术社区  · 7 年前

    我有一个奇怪的情况,我不知道如何解决它。

    我在Doctrine中得到了一个查询结果,格式看起来很奇怪。

    我使用Doctrine执行一个查询,如下例所示:

    $query = "select ... from ..."
    $stmt = $this->oracleDB->prepare($query); 
    (This oracleDB is configured in the config.yml with "charset: ISO-8859-1" and "driver: oci8")
    $stmt->bindValue(1, $variable, "...");
    more binds...
    $stmt->execute();
    $results = $stmt->fetchAll();
    

    大多数情况下,结果是正常的,json_encode可以工作,但有时我会得到类似下一个结果的结果:

    dump($results);
    array:1 [▼
      0 => array:6 [▼
        "BLA1" => "xx"
        "BLA2" => "XX"
        "BLA3" => "XX"
        "BLA4" => "XX"
        "BLA5" => b"XX"
        "BLA6" => "XX"
      ]
    ]
    var_dump($results);
    array (size=1)
      0 => 
        array (size=12)
          'BLA1' => string 'XX' (length=2)
          'BLA2' => string 'XX' (length=2)
          'BLA3' => string 'XX' (length=2)
          'BLA4' => string 'XX' (length=2)
          'BLA5' => string 'X�X' (length=3)
          'BLA6' => string 'XX' (length=2)
    
    dump(json_encode($results));
    false
    

    如果我取消设置bla5,json_编码工作得很好。

    unset($result[0]['BLA5']);
    
    dump($result);
    array:1 [▼
    0 => array:5 [▼
        "BLA1" => "xx"
        "BLA2" => "XX"
        "BLA3" => "XX"
        "BLA4" => "XX"
        "BLA6" => "XX"
      ]
    ]
    
    dump(json_encode($results));
    "[{"BLA1":"XX","BLA2":"XX","BLA3":"XX","BLA4":"XX","BLA6":"XX"}] ◀"
    

    有什么想法吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Ricard Espinàs Llovet    7 年前

    我原以为数据库里有字符集ISO-8859-1,但实际上没有,所以一切都在控制之中。谢谢!