Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to import multiple json file into mysql database at same time?

I have a database , that cantain about 10 column. each column should get values from seprate json file. how i can do that at same time?

for example :

column1 ------ file1.json
column2 ------ file2.json
column3 ------ file3.json
column4 ------ file4.json

my php code is something like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?php


    //connect to mysql db
    $con = mysql_connect("username","password","") or die('Could not connect: ' . mysql_error());
    //connect to the employee database
    mysql_select_db("employee", $con);

    //read the json file contents
    $jsondata = file_get_contents('file1.json');
    
    //convert json object to php associative array
    $data = json_decode($jsondata, true);
    
    //get the employee details
    $firstcolumn = $data['sample-column'];
  

    
    //insert into mysql table
    $sql = "INSERT INTO tbl_emp(sample-column, )
    VALUES('$id', ')";
    if(!mysql_query($sql,$con))
    {
        die('Error : ' . mysql_error());
    }
?>

I dont have any result

>Solution :

You can use a loop to read each file and insert the data into the database. Here is an example of how this can be done using PHP:

<?php
//connect to mysql db
$con = mysql_connect("username","password","") or die('Could not connect: ' . mysql_error());
//connect to the employee database
mysql_select_db("employee", $con);

//Loop through the files
$files = array('file1.json', 'file2.json', 'file3.json', 'file4.json');
foreach($files as $file) {
//read the json file contents
$jsondata = file_get_contents($file);

//convert json object to php associative array
$data = json_decode($jsondata, true);

//get the employee details
$columnData = $data['sample-column'];

//insert into mysql table
$sql = "INSERT INTO tbl_emp(sample-column) VALUES('$columnData')";
if(!mysql_query($sql,$con)) {
    die('Error : ' . mysql_error());
 }
}
?>

I hope this answer helps.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading