【问题标题】:Mysql insert null when variable is empty当变量为空时Mysql插入null
【发布时间】: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); 

【问题讨论】:

    标签: php mysql json


    【解决方案1】:

    你有错别字

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

    不是$query1,而是$query

    【讨论】:

    • 这发生在我们最好的人身上。仔细阅读错误信息会有很大帮助。
    猜你喜欢
    • 1970-01-01
    • 2011-11-19
    • 2013-09-13
    • 2014-09-10
    • 2011-06-04
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多