Search code examples
phpandroidjsonnullreturn

Json return null


I have the next problem doing a json to read in Andorid (the credentials are hidden but connection going good in others files)

class reportes
{
    var $parametro;
    var $conexion;

    function __construct(){
        $host = "IP"; $DBName = "DbName";
        $usuario="user"; $contrasena="pass";

        $driver = "DRIVER={iSeries Access ODBC Driver};
                   SYSTEM=$host;Uid=$usuario;
                   Pwd=$contrasena;Client_CSet=UTF-8;";
        $this->conexion = odbc_connect($driver, $usuario, $contrasena);
    }


    function consulta($parametro){

    $query=
    "SELECT OHSNME,OHTOT$,OHREPÑ
    FROM MYDB.SANFPRD.FOMHDR
    WHERE OHORDÑ= $parametro ";


    echo $query."<br><br>";

    if ($this->conexion == 0) {echo "Ha fallado la conexion a la BBDD </br>";}
        else{
            $datos = array();
            $result=odbc_exec($this->conexion,$query);
            while($row = odbc_fetch_object($result)){
                $datos[]= $row;
            }
            echo json_encode($datos);
    }

    }//Fin funcion consulta()

}//Fin de la clase

$consultar = new reportes();

$nota_venta = $_REQUEST['parametro'];
$consultar->consulta($nota_venta);

the response JSON that i get is:

SELECT OHSNME,OHTOT$,OHREPÑ FROM DELLORTO.SANFPRD.FOMHDR WHERE OHORDÑ= 366 

[{"OHSNME":"E.C. GM. ","OHTOT$":"1861.00",null:" A07"}]

you can see that OHORDÑ is probably the problem with the 'Ñ' but this table are part a productive database and i can't update


Solution

  • Solution #1, alias the column name to a name without non-ascii characters:

    $query=
    "SELECT OHSNME,OHTOT$,OHREPÑ AS OHREPN
    FROM MYDB.SANFPRD.FOMHDR
    WHERE OHORDÑ= $parametro ";
    

    Solution #2, manually serialize using utf8_encode():

            $result=odbc_exec($this->conexion,$query);
            while($row = odbc_fetch_object($result)){
                $_row_fix = array();
                foreach ($row as $field => $val) {
                  $_row_fix[utf8_encode($field)] = utf8_encode($val);
                }
                $datos[]= $_row_fix;
            }