如何使用php将多个表记录从json导入mysql
我有来自多个表多记录的json文件。json文件是从本地mysql数据库创建的,我还有另一个具有相同模式的在线数据库。但并非每个表都有相同数量的字段和相同的数据/值。但是所有表的一些字段名都是相同的(id、added\u on、last\u updated)。我想通过执行一个php文件将这些json文件值导入在线数据库 json文件如下所示:如何使用php将多个表记录从json导入mysql,php,mysql,json,Php,Mysql,Json,我有来自多个表多记录的json文件。json文件是从本地mysql数据库创建的,我还有另一个具有相同模式的在线数据库。但并非每个表都有相同数量的字段和相同的数据/值。但是所有表的一些字段名都是相同的(id、added\u on、last\u updated)。我想通过执行一个php文件将这些json文件值导入在线数据库 json文件如下所示: [ { "tableName":"table_Name_1", "rows":[
[
{
"tableName":"table_Name_1",
"rows":[
{
"t1column1":"valuet1row11",
"t1column2":"valuet1row12",
"t1columnx":"valuet1row1x"
},
{
"t1column1":"valuet1row21",
"t1column2":"valuet1row21",
"t1columnx":"valuet1row2x"
},
{
"t1column1":"valuet1rowx1",
"t1column2":"valuet1rowx2",
"t1columnx":"valuet1rowxx"
}
]
},
{
"tableName":"table_Name_2",
"rows":[
{
"t2column1":"valuet2row11",
"t2column2":"valuet2row12",
"t2columnx":"valuet2row1x"
},
{
"t2column1":"valuet2row21",
"t2column2":"valuet2row22",
"t2columnx":"valuet2row2x"
},
{
"t2column1":"valuet2rowx1",
"t2column2":"valuet2rowx2",
"t2columnx":"valuet2rowxx"
}
]
},
{
"tableName":"table_Name_n",
"rows":[
{
"tncolumn1":"valuetnrow11",
"tncolumn2":"valuetnrow12",
"tncolumnx":"valuetnrow1x"
},
{
"tncolumn1":"valuetnrow21",
"tncolumn2":"valuetnrow22",
"tncolumnx":"valuetnrow2x"
},{
"tncolumn1":"valuetnrowx1",
"tncolumn2":"valuetnrowx2",
"tncolumnx":"valuetnrowxx"
}
]
},
]
下面的php代码用于将json文件记录导入数据库中的单个表。(这里single_table.json只包含一个表记录)
这只是一个框架,展示了基于JSON中的数据构建查询的想法。这需要 根据实际数据和表模式进行一些调整 作为改进,我还将为每个表构建一个具有多个值的INSERT 编辑 将代码更改为读取并插入JSON文件中的所有表。假定JSON文件中的表名和列名与实际表匹配。它还为具有id列的表添加重复键更新 注意:未测试,可能有打字错误
<?php
try
{
$connect = mysqli_connect("localhost", "fmart", "password", "mart_dbsync");
$query = '';
$table_data = '';
$filename = "single_table.json";
$data = file_get_contents($filename);
$array = json_decode($data, true);
foreach($array as $set)
{
$tblName = $set['tableName'];
if(sizeof($set['rows']) > 0) {
$query = '';
$colList = array();
$valList = array();
// Get list of column names
foreach($set['rows'][0] as $colname => $dataval) {
$colList[] = "`".$colName."`";
}
$query .= "INSERT INTO `".$tblName."` \n";
$query .= "(".implode(",",$colList).")\nVALUES\n";
// Go through the rows for this table.
foreach($set['rows'] as $idx => $row) {
$colDataA = array();
// Get the data values for this row.
foreach($row as $colName => $colData) {
$colDataA[] = "'".$colData."'";
}
$valList[] = "(".implode(",",$colDataA).")";
}
// Add values to the query.
$query .= implode(",\n",$valList)."\n";
// If id column present, add ON DUPLICATE KEY UPDATE clause
if(in_array("`id`",$colList)) {
$query .= "ON DUPLICATE KEY UPDATE\n\tSet ";
$tmp = array();
foreach($colList as $idx => $colName) {
$tmp[] = $colName." = new.".$colname." ";
}
$query .= implode(",",$tmp)."\n";
} else {
echo "<p><b>`id`</b> column not found. <i>ON DUPLICATE KEY UPDATE</i> clause <b>NOT</b> added.</p>\n";
echo "<p>Columns Found:<pre>".print_r($colList,true)."</pre></p>\n";
}
echo "<p>Insert query:<pre>$query</pre></p>";
$r = mysqli_query($connect, $query);
echo "<h1>".mysqli_num_rows($r)." Rows appeded in $tblName</h1>";
} else {
echo "<p>No rows to insert for $tblName</p>";
}
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
为什么不使用行数组中的列为每个表生成查询呢。一个包含JSON中的列名和数据表列名的简单数组将允许您在浏览JSON时动态构建整个INSERT语句。我找不到它。一些id
已经存在,因此我想更新它们,而不是INSERT
。我应该做些什么修改?查找唯一索引和插入。。。在重复键
.FYI上,我已经链接了json记录。根据你的代码,我在哪里可以在重复键上应用。这是INSERT语句语法的一部分。上面的代码只是向您展示如何构造代码。
contacts
Insert query:
INSERT INTO contacts
(First_Name, Last_Name, Company, Business_Phone, Email_Address)
VALUES
('Dave','Frank','Company1','0115 999999','zvv@zz.com'),
('Dave','Blogs','Company2','0115 888888','zvv@zz.com'),
('David','frank','Company3','0115 777777','zvv@zz.com')
Error: 1
INSERT INTO contacts (First_Name, Last_Name, Company, Business_Phone, Email_Address) VALUES ('Dave','Frank','Company1','0115 999999','zvv@zz.com'), ('Dave','Blogs','Company2','0115 888888','zvv@zz.com'), ('David','frank','Company3','0115 777777','zvv@zz.com')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1contacts_old
Insert query:
INSERT INTO contacts_old
(First_Name, Last_Name, Company, Business_Phone, Email_Address)
VALUES
('Dave','Frank','Company1','0115 999999','zvv@zz.com'),
('Dave','Blogs','Company2','0115 888888','zvv@zz.com'),
('David','frank','Company3','0115 777777','zvv@zz.com')
Error: 1
INSERT INTO contacts_old (First_Name, Last_Name, Company, Business_Phone, Email_Address) VALUES ('Dave','Frank','Company1','0115 999999','zvv@zz.com'), ('Dave','Blogs','Company2','0115 888888','zvv@zz.com'), ('David','frank','Company3','0115 777777','zvv@zz.com')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
<?php
try
{
$connect = mysqli_connect("localhost", "fmart", "password", "mart_dbsync");
$query = '';
$table_data = '';
$filename = "single_table.json";
$data = file_get_contents($filename);
$array = json_decode($data, true);
foreach($array as $set)
{
$tblName = $set['tableName'];
if(sizeof($set['rows']) > 0) {
$query = '';
$colList = array();
$valList = array();
// Get list of column names
foreach($set['rows'][0] as $colname => $dataval) {
$colList[] = "`".$colName."`";
}
$query .= "INSERT INTO `".$tblName."` \n";
$query .= "(".implode(",",$colList).")\nVALUES\n";
// Go through the rows for this table.
foreach($set['rows'] as $idx => $row) {
$colDataA = array();
// Get the data values for this row.
foreach($row as $colName => $colData) {
$colDataA[] = "'".$colData."'";
}
$valList[] = "(".implode(",",$colDataA).")";
}
// Add values to the query.
$query .= implode(",\n",$valList)."\n";
// If id column present, add ON DUPLICATE KEY UPDATE clause
if(in_array("`id`",$colList)) {
$query .= "ON DUPLICATE KEY UPDATE\n\tSet ";
$tmp = array();
foreach($colList as $idx => $colName) {
$tmp[] = $colName." = new.".$colname." ";
}
$query .= implode(",",$tmp)."\n";
} else {
echo "<p><b>`id`</b> column not found. <i>ON DUPLICATE KEY UPDATE</i> clause <b>NOT</b> added.</p>\n";
echo "<p>Columns Found:<pre>".print_r($colList,true)."</pre></p>\n";
}
echo "<p>Insert query:<pre>$query</pre></p>";
$r = mysqli_query($connect, $query);
echo "<h1>".mysqli_num_rows($r)." Rows appeded in $tblName</h1>";
} else {
echo "<p>No rows to insert for $tblName</p>";
}
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>