Error Array to String Conversion

0

I have an Android application that sends a Json in the form of a string to the server in PHP. I tested with var_dump to verify that the data was being passed correctly and everything is ok. The problem is that when I try to access json and assign values from a nested array of the main object to an array in PHP, I have an error trying to include this array in Mysql. Before doing this inclusion, I tested only PHP and MySQL and everything is working perfectly.

if  (!empty($_POST)){
   $info = file_get_contents('php://input');
   $json = json_decode($info, true);
   $login= "";
   $utilizaExercicio = array();
  //var_dump($info);

   foreach($json['Paciente'][0] as $nome){
      $login = $nome;
   }


   foreach ($json['Paciente'][1]as $exercicio){
       $utilizaExercicio[] = array($exercicio);
   }

for ($i=0; sizeOf($utilizaExercicio) > $i; $i++){

    $exercicios = mysqli_fetch_array($ordernar);
    $sql1 = ("UPDATE exercicio_paciente
             INNER JOIN pacientes ON (exercicio_paciente.idpaciente =  pacientes.ID)
             INNER JOIN exercicios ON (exercicios.idexercicios = exercicio_paciente.idexercicio)
             SET exercicio_paciente.utilizar_exercicio=$utilizaExercicio[$i]     
             WHERE exercicio_paciente.idexercicio= {$exercicios['idexercicio']} AND
                   pacientes.ID=(SELECT c.ID FROM (SELECT * FROM pacientes) as c 
                                 WHERE c.login_paciente = '$login');");

    $salvo = mysqli_query($connect, $sql1);

    if ($salvo)
        $sucesso = 1;
    else
        $sucessoLocal=0;

}
}

Here, the error happens in SET exercicio_paciente.utilizar_exercicio=$utilizaExercicio[$i]

And my json:

{Paciente:[{"Nome":"Rafael"},
           {Exercicios:[{"0":"1"},
                        {"1":"0"},
                        {"2":"0"}]}]
}

What would be wrong? NOTE: The JSON framework I set up here because I could not move to a specific file, but I believe it is correct.

    
asked by anonymous 29.01.2018 / 18:30

1 answer

1

You can not concatenate a array (exercise array) with a string . When you need to transform array to string to save to the database, you can use one of the three methods below.

Using json_encode

You use the following code json_encode($utilizaExercicio[$i]);

exercicio_paciente.utilizar_exercicio=\"".json_encode($utilizaExercicio[$i])."\"

With this function you will have a query similar to this:

SET exercicio_paciente.utilizar_exercicio="[[["1"],{"1":"0"},{"2":"0"}]]"

Using serialize :

You use the following code serialize($utilizaExercicio[$i]);

exercicio_paciente.utilizar_exercicio=\"".serialize($utilizaExercicio[$i])."\"

With this function you will have a query similar to this:

SET exercicio_paciente.utilizar_exercicio="a:1:{i:0;a:3:{i:0;a:1:{i:0;s:1:"1";}i:1;a:1:{i:1;s:1:"0";}i:2;a:1:{i:2;s:1:"0";}}}"

Using print_r :

You use the following code print_r($utilizaExercicio[$i], true)

exercicio_paciente.utilizar_exercicio=\"".print_r($utilizaExercicio[$i], true)."\"

With this function you will have a query similar to this:

SET exercicio_paciente.utilizar_exercicio="Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => 1
                )

            [1] => Array
                (
                    [1] => 0
                )

            [2] => Array
                (
                    [2] => 0
                )

        )

)"
;
    
29.01.2018 / 18:54