首先,我想将从谷歌API获得的数据插入MySQL。我可以将数据插入我的数据库中。但我遇到了一个问题,如果数据相同,它将不会取代旧数据。我想要替换的数据是片段,但如果链接相同但片段不同,则会创建新的而不是替换。我已经尝试过使用INSERT IGNORE INTO,但它都插入新数据而不是替换它,我使用REPLACE INTO但它也一样。这是我的编码。
<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 = 'https://www.googleapis.com/customsearch/v1?q=unimap&cx=004123968310343535430%3Aaxml6iel9yo&key=AIzaSyDxPcphnrcN9_dfkRkFTbwkv44m1-HI1Hg&sort=date';
$data = file_get_contents($filename); //Read the JSON file in PHP
$array = json_decode($data, true); //Convert JSON String into PHP Array
//$record =$array['items'][1]["link"];
// var_dump($record);
foreach($array['items'] as $row) //Extract the Array Values by using Foreach Loop
{
// $query .="SELECT * from go WHERE htmlSnippet ='"
$query .= "INSERT INTO go(htmlTitle, Link, htmlSnippet) VALUES ('".$row["htmlTitle"]."', '".$row["link"]."', '".$row["htmlSnippet"]."') ;"; // Make Multiple Insert Query
$table_data .= '
<tr>
<td>'.$row["htmlTitle"].'</td>
<td>'.$row["link"].'</td>
<td>'.$row["htmlSnippet"].'</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>