Convert result Json objects in List Json ?, using php and mysql

0

PHP code to show results of a query to MySQL (returns json object)

if ($result = $mysqli->query("SELECT ID,NOMBRE,NICK,PASSWORD,EMAIL FROM  ud_Usuario")) {
    // check for empty result
    if (mysqli_num_rows($result) > 0) {
        // looping through all results
        // products node
        $response["success"] = 1;
        $response["message"] = "ok";
        $response["Usuario"] = array();

        while ($row = mysqli_fetch_array($result)) {
            // temp user array
            $USUARIO = array();
            $USUARIO["id"] = $row["ID"];
            $USUARIO["nombre"] = $row["NOMBRE"];
            $USUARIO["nick"] = $row["NICK"];
            $USUARIO["password"] = $row["PASSWORD"];
            $USUARIO["email"] = $row["EMAIL"];
            array_push($response["Usuario"], $USUARIO);
        }
        echo json_encode($response);
    } else {
        // no products found
        $response["success"] = 0;
        $response["message"] = "No Usuarios";
        // echo no users JSON
        echo json_encode($response);
    }

    /* liberar el conjunto de resultados */
    $result->close();
}

Result is.

{"success":1,"message":"ok","Usuario":[{"id":"1","nombre":"G_nom","nick":"J_nick","password":"J_pass"},{"id":"2","nombre":"U_nom","nick":"U_nick","password":"U_pass"}]}

As you can see, it's a JSON object.

What I'm asking is how to get that JSON object back into a list json .

    
asked by Gerard Cáceres 16.10.2018 в 17:49
source

1 answer

0

to return a json list. you should change the php code in this way.

if ($result = $mysqli->query("SELECT ID,NOMBRE,NICK,PASSWORD,EMAIL FROM  ud_Usuario")) {


    $rawdata = array(); //creamos un array

    //guardamos en un array multidimensional todos los datos de la consulta
    $i=0;

    while($row = mysqli_fetch_array($result))
    {
        $rawdata[$i] = $row;
        $i++;
    }



    echo json_encode($rawdata);

    $result->close();
}

will return the next json

[{"0":"1","ID":"1","1":"G_nom","NOMBRE":"G_nom","2":"J_nick","NICK":"J_nick","3":"J_pass","PASSWORD":"J_pass","4":"J_email","EMAIL":"J_email"},{"0":"2","ID":"2","1":"U_nom","NOMBRE":"U_nom","2":"U_nick","NICK":"U_nick","3":"U_pass","PASSWORD":"U_pass","4":"U_email","EMAIL":"U_email"}]

The only observation is that self generates a value "0",

    
answered by 16.10.2018 / 18:11
source