Import json file into a MySQL database?

1

Well I'm started in the database study and I came across the following situation I have this json file with the following data:

[{"nome":"José","ddd":27,"telefone":21239990,"cpf":"11111111111","sabe_programar":true},{"nome":"João","ddd":27,"telefone":21239990,"cpf":"22222222222","sabe_programar":false},{"nome":"Maria","ddd":27,"telefone":21239990,"cpf":"22222222222","sabe_programar":true},{"nome":"Antônio","ddd":27,"telefone":21239990,"cpf":"22222222222","sabe_programar":false}]

Well I already managed to pass it to php, with this code:

<?php
$arquivo = file_get_contents('pessoas.json');
$json = json_decode($arquivo);
foreach($json as $registro):
    echo 'nome: ' . $registro->nome . ' - ddd: ' . $registro->ddd . ' - telefone: ' . $registro->telefone . ' - cpf '. $registro->cpf . ' - sabe_programar' . $registro->sabe_programar . '<br>';
endforeach;

My question is how do I now import this data into a MySQL database? (I've already created a database, and a table)

    
asked by anonymous 16.09.2017 / 15:50

1 answer

2

Hello! Then do the following, transform Json to array and throw the data from each table to a separate array:

$meu_array = json_decode($var_do_jason, true);
$n = $meu_array["nome"];
$c = $meu_array["cpf"];
$r = $meu_array["rg"]; 

Then use a for to insert the elements into the base:

for(i=0;$meu_array>i;i++){
$nome = $n[i];
$cpf = $c[i];
$rg = $r[i];
            mysql_query("INSERT INTO tabela (nome, cpf, rpg) VALUES('$nome', '$cpf', '$rg'"));

        }
    
16.09.2017 / 16:37