Send JSON php

1

I am having problems sending a JSON from a PHP and MySQL webService, I leave the code of the query and the fetch assoc, the query has already been checked and it is correct, in phpMyAdmin it makes me perfect, the problem is that when I receive the JSON on Android I receive things that should not be part of the JSON:

$consulta= "SELECT 'id','actividad','lugar','fecha' FROM 'planes'  WHERE 'usuario'= '".$usuario."' ORDER BY 'id' DESC";

$result = mysqli_query($mysqli, $consulta);   
$rawdata = array();
$i=0;

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

echo json_encode($rawdata);

and this is what I get on android according to the log:

  

{"0": "1", "id": "1", "1": "Test", "activity": "Test", "2": "Madrid", "place": "Madrid "," 3 ":" 01/12/2019 "," date ":" 01/12/2019 "}

is putting things that are not, should be

  

{"id": "1", "activity": "Test", "place": "Madrid", "date": "01/12/2019"}

I do not know if it's the bars (/), if it's the utf8 encoding, ...

Any ideas?

Thank you very much, best regards

    
asked by midlab 21.11.2018 в 19:33
source

2 answers

1

The function is correct, since mysqli_fetch_array($result) is equivalent to using mysqli_fetch_array($result, MYSQLI_BOTH) that returns the names of the columns with their respective values and the positions of the columns with their values.

If you want mysqli_fetch_array() to return only the names with the values I recommend you add MYSQLI_ASSOC as the second parameter:

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
    $rawdata[] = $row;
}

Notice that we also do not use $i since when doing $rawdata[] = valor we are automatically entering data into the array.

    
answered by 21.11.2018 / 20:16
source
1

you can directly use array_push ($ rawdata, $ row);

    
answered by 22.11.2018 в 00:46