Create json array from php + mysql

0

I have the following code, but it does not show it to me as a json array, how can I modify it so that it is an array type array= [{"nombre":"jesus"}] etc etc.

 $result = mysqli_query($connect, "SELECT * FROM 'transacciones' WHERE 'Usercorreo' = '$Username'");
    $existsql =  mysqli_affected_rows($connect);
    if ($existsql > 0) {
        $resultado = mysqli_query($connect, "SELECT * FROM 'transacciones' WHERE 'Usercorreo' = '$Username' AND tipo='$tipo'");
        while($array=mysqli_fetch_array($resultado)){
            $response["idTrans"] = $array['idTrans'];
            //$response["ComCorreo"] = $array['ComCorreo'];
            $ComCorreo = $array['ComCorreo'];
            $response["monto"] = $array['monto'];
            $response["fecha"] = $array['fecha'];
            $response["concepto"] = $array['concepto'];
            //echo json_encode($response);
            $comrsql=mysqli_query($connect,"SELECT *  FROM 'comercios' WHERE 'Comcorreo'='$ComCorreo'");
                        while($rowid=mysqli_fetch_array($comrsql)){
                            $response["COMERCIONOMBRE"] =$rowid['nombre'];
                            $response["COMERCIORIF"] =$rowid['rif'];
                            $response["COMERCIOLOGO"] =$rowid['logo'];
                            echo json_encode($response);
                        }
        }
    } else {
        $response["success"]=false;
        $response["error"] = "NOHAYTRANSACCIONES";
    }

    $response["success"]=true; 

    mysqli_close($connect);

At this moment the result is structured like this:

    {
    "idTrans": "1",
    "monto": "5998.89",
    "fecha": "2017-08-15",
    "concepto": "",
    "COMERCIONOMBRE": "Fitness Universal",
    "COMERCIORIF": "402123693",
    "COMERCIOLOGO": "logo\/1.png"
}

but it is not how the array should show me, that I must modify in my code taking into account that I consult two tables instead of one.

    
asked by Jesus Moran 25.08.2017 в 02:27
source

1 answer

1

To get the result you are looking for, you must create the object in the following way and then use the json_encode.

while($rowid=mysqli_fetch_array($comrsql)){
    $response[0] = [
        "COMERCIONOMBRE"    => $rowid['nombre'],
        "COMERCIORIF"       => $rowid['rif'],
        "COMERCIOLOGO"      => $rowid['logo']
    ]   
    echo json_encode($response);
}

So that when Json returns, he will return it to you in the format you want, I hope it will be useful for you and if so, do not forget to leave your vote.

    
answered by 25.08.2017 / 02:59
source