Reputation: 567
I am trying to insert data into a database from an external json url. Some of the json has empty variables in php. I am trying to get it to put NULL or N/A if there is nothing in the variable.
Here is what I could think up from reading around but is currently not working.
mysqli_query(): Empty query in update.php on line 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);
Upvotes: 1
Views: 151
Reputation: 1996
you have a typo
$q = mysqli_query($con, $query) or die (mysqli_error($con));
not $query1
but $query
Upvotes: 1