1

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); 

1 Answer 1

1

you have a typo

$q = mysqli_query($con, $query) or die (mysqli_error($con));

not $query1 but $query

Sign up to request clarification or add additional context in comments.

1 Comment

It happens to best of us. Reading the error message carefully helps a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.