我已经用JSON获取数据并插入MySQL。但是如果数据类似于数组中的数组,我就遇到了问题。我自己尝试了很多方法,但我不知道如何正确地做到
这是index.php
<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"
}
]
}
答案 0 :(得分:1)
你可以试试这个
<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", "", "test_db"); //Connect PHP to MySQL Database
$connect->set_charset("utf8");
$query = '';
$table_data = '';
$filename = "https://www.googleapis.com/customsearch/v1?key=AIzaSyCfaTE3WZtumt7gPas7Ey2GTKqWoJ2oH1M&cx=e838f67a0ac798369&q=Abir%20Abedin%20Khan";
$data = file_get_contents($filename); //Read the JSON file in PHP
$array = json_decode($data, true); //Convert JSON String into PHP Array
foreach($array['items'] as $row) //Extract the Array Values by using Foreach Loop
{
$query .= "INSERT INTO search_engine(title, blurb, url) VALUES ('".$row["title"]."', '".$row["snippet"]."', '".$row["link"]."'); "; // Make Multiple Insert Query
$table_data .= '
<tr>
<td>'.$row["title"].'</td>
<td>'.$row["snippet"].'</td>
<td>'.$row["link"].'</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%">Title</th>
<th width="10%">Blurb</th>
<th width="45%">Link</th>
</tr>
';
echo $table_data;
echo '</table>';
}
?>
<br />
</div>
</body>
</html>