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:
<?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
CodePudding user response:
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.