【发布时间】:2016-05-16 23:29:06
【问题描述】:
我正在尝试从外部 json url 将数据插入数据库。一些 json 在 php.ini 中有空变量。如果变量中没有任何内容,我试图让它放置 NULL 或 N/A。
这是我通过阅读可以想到但目前无法正常工作的内容。
mysqli_query():update.php 第 33 行的空查询
include('db.php');
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "bot.notenoughmods.com/1.8.9.json");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
// $output contains the output string
$json = json_decode(curl_exec($ch), true);
function isitempty($val){
if (trim($val) === ''){$val = "NULL";}
return $val;
}
foreach($json as $item) {
$name = isitempty($item['name']);
$version = isitempty($item['version']);
$author = isitempty($item['author']);
$link = isitempty($item['longurl']);
$comment = isitempty($item['comment']);
$repo = isitempty($item['repo']);
$license = isitempty($item['license']);
$time = isitempty($item['lastupdated']);
foreach($item['dependencies'] as $dep) {
$dep2 = isitempty($dep);
$query = "INSERT INTO mods (name,dependancies,version,author,link,repo,license,update_time) VALUES ('". $name."','". $dep2 ."','". $version."','". $author."','". $link."','". $repo ."','". $license ."','". $update_time."')";
$q = mysqli_query($con, $query1) or die (mysqli_error($con));
}
}
// close curl resource to free up system resources
curl_close($ch);
【问题讨论】: