我正在尝试从外部json url将数据插入数据库。一些json在php中有空变量。如果变量中没有任何内容,我试图让它放入NULL或N / A.
这是我从阅读中想到的,但目前无法正常工作。
mysqli_query():第33行的update.php中的空查询
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);
答案 0 :(得分:1)
你有一个错字
$q = mysqli_query($con, $query) or die (mysqli_error($con));
不是$query1
而是$query