Array to String Conversion Error

Asked

Viewed 1,023 times

0

I have an Android application that sends a string-shaped Json to the PHP server. I tested with var_dump to check that the data was being passed correctly and everything is ok. The problem is that when trying to access json and assign values from a nested array of the main object to a PHP array, I have an error in trying to include this array in Mysql. Before making 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 structure of JSON I built here, because I could not pass to a specific file, but I believe it is correct.

1 answer

1

You can’t concatenate a array (array of exercises) with a string. When you need to turn array in string to save in 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
                )

        )

)"
;
  • Pardon me for asking, but would you still need to manipulate these entrances? I put the 3 and in all appeared the following error: "Cannot use Object of type mysqli_result as array". <br/> "". print_r($utilizaExercicio[$i], true)."" The tabs would be positioned correctly]?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.