我使用php脚本将json供稿导入到数据库中。 json提要具有多个页面。像:www.website.com/file.json/?page=1、www.website.com/file.json/?page=2,依此类推。在每个json页面上都存储了10条评论。该脚本将所有页面保存到名为“ json-pages”的本地文件夹中。保存文件后,脚本将json页面(1.json,2.json)加载到数据库中。到目前为止一切正常。
不幸的是,当更改.json文件时,脚本未更新/添加数据库行。它仅在首次运行php脚本时有效。
例如,我有具有10条评论的文件1.json和具有2条评论的文件2.json。
今天,我们收到了两项新评论。然后我对文件2.json总共获得了4条评论。该脚本不会将数据库表从12行更新为14(新总数)行。
我在做什么错?我希望有人知道窍门。
这是我的脚本:
// API KEY
DEFINE('API_KEY','xxx'); // SET API KEY
// Setup database connection
include_once $_SERVER['DOCUMENT_ROOT'].'/wp-load.php';
$con = new mysqli(DB_HOST, DB_USER, DB_PASSWORD , DB_NAME);
mysqli_set_charset($con,"utf8");
// Check connection
if ($con->connect_error)
die("Connection failed: " . $con->connect_error);
global $wpdb;
$table_name = $wpdb->prefix . "custorate_beoordelingen";
// sql to create table
$sql = "CREATE TABLE $table_name (
`CustorateID` int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`title` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`date_created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`beoordeling` int(11) NOT NULL,
`aanbevolen` int(11) NOT NULL,
`tekst` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`klant_naam` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`reactie` int(11) NOT NULL
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci";
//print table status
if(mysqli_multi_query($con, $sql)) {
echo '<p style="text-align:center;"">Table created successfully</p>';
}
// created a folder with json-pages
DEFINE('DATA_FOLDER','./json-pages');
if (!is_dir(DATA_FOLDER)) {
mkdir(DATA_FOLDER, 0755, true);
}
// setup json file
$file = file_get_contents("https://www.custorate.nl/json/".API_KEY."/");
$data = json_decode($file, true);
// loop trough the number of pages
for($i=1; $i <= $data['meta']['total_pages']; $i++){
// use local data if exist, except the last one.
if(file_exists(DATA_FOLDER.'/'.$i.'.json') && $i < $data['meta']['total_pages']){
$file = file_get_contents(DATA_FOLDER.'/'.$i.'.json');
$query = '';
} else {
// download the file if not exist.
$file = file_get_contents("https://www.custorate.nl/json/".API_KEY."/?start=".$i."");
$query = '';
if($file){
$fp = fopen(DATA_FOLDER.'/'.$i.'.json',"w+");
fputs($fp, $file);
fclose($fp);
}
}
// change received data to an Array
$data = json_decode($file, true);
if(count($data['beoordelingen'])){
echo "<pre>";
echo "<p>-- SQL Query --</p>";
foreach($data['beoordelingen'] as $id=>$beoordeling){
$insertPairs = array();
foreach ($beoordeling as $key=>$val) {
$insertPairs[addslashes($key)] = addslashes($val);
}
$insertKeys = '`' . implode('`,`', array_keys($insertPairs)) . '`';
$insertVals = '"' . implode('","', array_values($insertPairs)) . '"';
$query .= "INSERT INTO {$table_name}({$insertKeys}) VALUES ({$insertVals}); ";
echo "INSERT INTO `{$table_name}` ({$insertKeys}) VALUES ({$insertVals});" . "\n";
} // end Foreach
echo "</pre>";
if(mysqli_multi_query($con, $query)) //Run Mutliple Insert Query
{
echo '<h3 style="text-align:center;"">JSON Data geïmporteerd</h3><br/>';
} //endif
} // endif
}// end loop