我已经在JSON中提取了数据并插入到MySQL中。但是如果数据像数组中的数组一样,我会遇到一个问题。我自己尝试了很多方法,但我不知道如何正确地进行
这是一个很好的例子。
<html>
<head>
<title>Media Monitoring</title>
<style>
.box
{
width:750px;
padding:20px;
background-color:#fff;
border:1px solid #ccc;
border-radius:5px;
margin-top:100px;
}
</style>
</head>
<body>
<div class="container box">
<h3 align="center">Import JSON File Data into Mysql Database in PHP</h3><br />
<?php
$connect = mysqli_connect("localhost", "root", "", "accounts"); //Connect PHP to MySQL Database
$query = '';
$table_data = '';
$filename = "json.json";
$data = file_get_contents($filename); //Read the JSON file in PHP
$array = json_decode($data, true); //Convert JSON String into PHP Array
foreach($array as $row) //Extract the Array Values by using Foreach Loop
{
$query .= "INSERT INTO employee(name, gender, gg) VALUES ('".$row["name"]."', '".$row["gender"]."', '".$row["gg"]."'); "; // Make Multiple Insert Query
$table_data .= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["gender"].'</td>
<td>'.$row["gg"].'</td>
</tr>
'; //Data for display on Web page
}
if(mysqli_multi_query($connect, $query)) //Run Mutliple Insert Query
{
echo '<h3>Imported JSON Data</h3><br />';
echo '
<table class="table table-bordered">
<tr>
<th width="45%">Name</th>
<th width="10%">Gender</th>
<th width="45%">Designation</th>
</tr>
';
echo $table_data;
echo '</table>';
}
?>
<br />
</div>
</body>
</html>
这是编辑过的
<html>
<head>
<title>Media Monitoring</title>
<style>
.box
{
width:750px;
padding:20px;
background-color:#fff;
border:1px solid #ccc;
border-radius:5px;
margin-top:100px;
}
</style>
</head>
<body>
<div class="container box">
<h3 align="center">Import JSON File Data into Mysql Database in PHP</h3><br />
<?php
$connect = mysqli_connect("localhost", "root", "", "accounts"); //Connect PHP to MySQL Database
$query = '';
$table_data = '';
$filename = "json.json";
$data = file_get_contents($filename); //Read the JSON file in PHP
$array = json_decode($data, true); //Convert JSON String into PHP Array
foreach($array as $row) //Extract the Array Values by using Foreach Loop
{
$query .= "INSERT INTO employee(name, gender, gg) VALUES ('".$row["items""name"]."', '".$row["items"]["gender"]."', '".$row["items"].["gg"]."'); "; // Make Multiple Insert Query
$table_data .= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["gender"].'</td>
<td>'.$row["gg"].'</td>
</tr>
'; //Data for display on Web page
}
if(mysqli_multi_query($connect, $query)) //Run Mutliple Insert Query
{
echo '<h3>Imported JSON Data</h3><br />';
echo '
<table class="table table-bordered">
<tr>
<th width="45%">Name</th>
<th width="10%">Gender</th>
<th width="45%">Designation</th>
</tr>
';
echo $table_data;
echo '</table>';
}
?>
<br />
</div>
</body>
</html>
工作的JSON数据
[
{
"name": "Rusydi",
"gender": "Male",
"gg": "System Architect"
},
{
"name": "Hakim",
"gender": "Male",
"gg": "Conservation worker"
}
]
数组中的JSON数据数组
{
"items": [
{
"name": "Rusydi",
"gender": "Male",
"gg": "System Architect"
},
{
"name": "Hakim",
"gender": "Male",
"gg": "Conservation worker"
}
]
}
1条答案
按热度按时间rqqzpn5f1#
你可以试试这个