【问题标题】:Inserting JSON file into MySQL via PHP Script通过 PHP 脚本将 JSON 文件插入 MySQL
【发布时间】:2016-01-29 13:51:20
【问题描述】:

我正在尝试通过 php 脚本将 JSON 文件中的数据添加到 MySQL 中,但我在 MySQL 中得到一个空数组并出现以下错误。

我在windp上使用xampp lite 1.8

注意:未定义变量:tagscolor in C:\xampp\htdocs\jsonreader\index.php 在第 64 行

注意:未定义变量:第 64 行 C:\xampp\htdocs\jsonreader\index.php 中的标记已修改

注意:第 66 行 C:\xampp\htdocs\jsonreader\index.php 中的数组到字符串转换 错误:添加到数据库中的问题您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册 在'order,description 附近使用正确的语法) VALUES('e','','','','','')' 在第 1 行

源代码 index.php

    <?php
require_once('includes/conn.php');

/* $fo=fopen("epl.json","r");
$fr=fread($fo,filesize("epl.json"));
$data=json_decode($fr,true); */

//read the json file contents
 $jsondata = file_get_contents('epl.json');
 //convert json object to php associative array
$data = json_decode($jsondata,true);
//var_dump($jsondata);
/* if($data)
{
    echo "AWESOME";
}
exit(); */
 //get the table details 
    $short_desc=$data['short_desc'];
    $original_id = $data['tags']['original_id'];
  $name = $data['tags']['name'];
    $color = $data['tags']['color'];
    $modified = $data['tags']['modified'];
$abstract = $data['tags']['abstract'];
$category = $data['tags']['category'];
$tag = $data['tags']['tag'];
$order_in_category = $data['tags']['order_in_category'];

 $short_desc=$data['short_desc'];
$id = $data['topics']['id'];
$user_id = $data['topics']['user_id'];
$postedby = $data['topics']['postedby'];
$publishDate = $data['topics']['publishDate'];
$photoUrl = $data['topics']['photoUrl'];
$description = $data['topics']['description'];
$title = $data['topics']['title'];
$addedOn = $data['topics']['addedOn'];
 $topic_rating = $data['topics']['topic_rating'];
$topic_rate_num = $data['topics']['topic_rate_num'];
$tags = $data['topics']['tags'];
$url = $data['topics']['url'];
$photosUrl = $data['topics']['photosUrl'];
$youtubeUrl = $data['topics']['youtubeUrl'];
 $topicsmodified = $data['topics']['modified'];
 $mainTag = $data['topics']['mainTag'];
 $topicscolor = $data['topics']['color'];
 $hashtag = $data['topics']['hashtag'];
 $captionUrl = $data['topics']['captionUrl'];
$players = $data['topics']['players']; 

 $players=$data['players'];
$original_id = $data['hashtags']['original_id'];
$name = $data['hashtags']['name'];
$color = $data['hashtags']['color'];
$order = $data['hashtags']['order'];
$description = $data['hashtags']['description'];




//insert into mysql table
  $sql1 = "INSERT INTO tags(short_desc, original_id, name,color, modified,abstract, category, tag, order_in_category)
VALUES('$short_desc','$original_id','$name', '$tagscolor','$tagsmodified','$abstract','$category','$tag','$order_in_category')";
$sql2 = "INSERT INTO topics(id,user_id,postedby,publishDate,photoUrl,description,title,addedOn,topic_rating,topic_rate_num,tags,url,photosUrl,youtubeUrl,modified,mainTag,color,hashtag,captionUrl,players)
VALUES('$id','$user_id','$postedby','$publishDate','$photoUrl','$description','$title','$addedOn','$topic_rating','$topic_rate_num','$tags','$url','$photosUrl','$youtubeUrl','$topicsmodified','$mainTag','$topicscolor','$hashtag','$captionUrl','$players')"; 
$sql3 = "INSERT INTO hashtags(players,original_id,name,color,order,description)
VALUES('$players','$original_id','$name','$color','$order','$description')";
if(!mysqli_query($conn,$sql1))
{
    die('Error :Problem adding into DB ' . mysqli_error($conn));
}
if(!mysqli_query($conn,$sql2))
{
    die('Error :Problem adding into DB ' . mysqli_error($conn));
}
if(!mysqli_query($conn,$sql3))
{
    die('Error :Problem adding into DB ' . mysqli_error($conn));
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Json reader</title>

    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
</head>

<body>

</body>
</html>

conn.php

    <?php
$host="localhost";
$user="root";
$pass="";
$db="soccer";
$conn=mysqli_connect($host,$user,$pass,$db) or die("No connection    exist".mysqli_error($conn));


?>

epl.json

{
"short_desc": [],
"tags": [
    {
        "original_id": "395",
        "name": "Social",
        "color": "#55acee",
        "modified": "1441458636",
        "abstract": "",
        "category": "TYPE",
        "tag": "TYPE_SOCIAL",
        "order_in_category": "34"
    },
    {
        "original_id": "3",
        "name": "Football",
        "color": "#008348",
        "modified": "1441458636",
        "abstract": "",
        "category": "SPORT",
        "tag": "TOPIC_SOCCER",
        "order_in_category": "4"
    },
    {
        "original_id": "394",
        "name": "Article",
        "color": "#f94500",
        "modified": "1441458636",
        "abstract": "",
        "category": "TYPE",
        "tag": "TYPE_ARTICLE",
        "order_in_category": "33"
    },
    {
        "original_id": "2",
        "name": "EPL",
        "color": "#005CAB",
        "modified": "1441458636",
        "abstract": "",
        "category": "LEAGUE",
        "tag": "LEAGUE_EPL",
        "order_in_category": "15"
    },
    {
        "original_id": "31",
        "name": "Manchester United",
        "color": "#DA020E",
        "modified": "1441458636",
        "abstract": "",
        "category": "TEAM",
        "tag": "TEAM_MANCHESTER_UNITED",
        "order_in_category": "2"
    },
    {
        "original_id": "619",
        "name": "Cristiano Ronaldo",
        "color": "#3239c7",
        "modified": "1441458636",
        "abstract": "",
        "category": "THEME",
        "tag": "THEME_CRISTIANO_RONALDO",
        "order_in_category": "8"
    },
    {
        "original_id": "546",
        "name": "Breaking News",
        "color": "#3d9ab4",
        "modified": "1441458636",
        "abstract": "",
        "category": "THEME",
        "tag": "THEME_BREAKING_NEWS",
        "order_in_category": "1"
    },
    {
        "original_id": "34",
        "name": "Real Madrid",
        "color": "#173980",
        "modified": "1441458636",
        "abstract": "",
        "category": "TEAM",
        "tag": "TEAM_REAL_MADRID",
        "order_in_category": "6"
    },
    {
        "original_id": "566",
        "name": "International Football",
        "color": "#e52e13",
        "modified": "1441458636",
        "abstract": "",
        "category": "THEME",
        "tag": "THEME_INTERNATIONAL_FOOTBALL",
        "order_in_category": "23"
    },
    {
        "original_id": "612",
        "name": "Argentina",
        "color": "#efae47",
        "modified": "1441458636",
        "abstract": "",
        "category": "THEME",
        "tag": "THEME_ARGENTINA",
        "order_in_category": "1"
    },
    {
        "original_id": "687",
        "name": "Sergio Aguero",
        "color": null,
        "modified": "1441886413",
        "abstract": "",
        "category": "THEME",
        "tag": "THEME_SERGIO_AGUERO",
        "order_in_category": "16"
    },
    {
        "original_id": "444",
        "name": "Barcelona",
        "color": "#A2214B",
        "modified": "1441458636",
        "abstract": "",
        "category": "TEAM",
        "tag": "TEAM_FC_BARCELONA",
        "order_in_category": "8"
    },
    {
        "original_id": "547",
        "name": "Transfer Window",
        "color": "#0e9796",
        "modified": "1441458636",
        "abstract": "",
        "category": "THEME",
        "tag": "THEME_TRANSFER_WINDOW",
        "order_in_category": "4"
    },
    {
        "original_id": "548",
        "name": "Artur Davtyan",
        "color": "#1c87a1",
        "modified": "1441458636",
        "abstract": "",
        "category": "THEME",
        "tag": "THEME_ARTUR_DAVTYAN",
        "order_in_category": "5"
    },
    {
        "original_id": "577",
        "name": "Serge Deble",
        "color": "#cd8bbe",
        "modified": "1441458636",
        "abstract": "",
        "category": "THEME",
        "tag": "THEME_SERGE_DEBLE",
        "order_in_category": "10"
    },
    {
        "original_id": "549",
        "name": "Marin Leovac",
        "color": "#790470",
        "modified": "1441458636",
        "abstract": "",
        "category": "THEME",
        "tag": "THEME_MARIN_LEOVAC",
        "order_in_category": "6"
    },
    {
        "original_id": "711",
        "name": "Philippe Coutinho",
        "color": null,
        "modified": "1442221206",
        "abstract": "",
        "category": "THEME",
        "tag": "THEME_PHILIPPE_COUTINHO",
        "order_in_category": "24"
    },
    {
        "original_id": "30",
        "name": "Arsenal",
        "color": "#EF0107",
        "modified": "1441458636",
        "abstract": "",
        "category": "TEAM",
        "tag": "TEAM_ARSENAL",
        "order_in_category": "1"
    }
],
"topics": [
    {
        "id": "86820",
        "user_id": "8003",
        "postedby": "Admin Administrator",
        "publishDate": "2015-10-08T08:30:38+3:00",
        "photoUrl": "http://img.bleacherreport.net/img/images/photos/003/531/421/hi-res-afcc062b0248a8903713f20fdaf4f6a2_crop_north.jpg?w=650&h=440&q=75",
        "description": "",
        "title": "Cristiano Ronaldo Pays Tribute to 'Father Figure' Sir Alex Ferguson",
        "addedOn": "2015-10-09T09:59:01+3:00",
        "topic_rating": "0",
        "topic_rate_num": "0",
        "tags": [
            "TOPIC_SOCCER",
            "TYPE_ARTICLE",
            "LEAGUE_EPL",
            "TEAM_MANCHESTER_UNITED",
            "THEME_CRISTIANO_RONALDO",
            "THEME_BREAKING_NEWS",
            "TEAM_REAL_MADRID"
        ],
        "url": "http://bleacherreport.com/articles/2577078-cristiano-ronaldo-pays-tribute-to-father-figure-sir-alex-ferguson",
        "photosUrl": "",
        "youtubeUrl": "",
        "modified": "1444373946",
        "mainTag": "TYPE_ARTICLE",
        "color": "#f94500",
        "hashtag": "Article",
        "captionsUrl": "",
        "players": []
    },
    {
        "id": "85087",
        "user_id": "8003",
        "postedby": "Admin Administrator",
        "publishDate": "2015-10-07T13:28:38+3:00",
        "photoUrl": "http://img.bleacherreport.net/img/images/photos/003/531/080/hi-res-cb95763e1709d4d1e71198b525af84ff_crop_north.jpg?w=650&h=440&q=75",
        "description": "",
        "title": "Leo Messi Tells Sergio Aguero He Has to Wear His No. 10 Shirt While He's Out",
        "addedOn": "2015-10-09T09:59:01+3:00",
        "topic_rating": "0",
        "topic_rate_num": "0",
        "tags": [
            "TOPIC_SOCCER",
            "TYPE_ARTICLE",
            "LEAGUE_EPL",
            "THEME_INTERNATIONAL_FOOTBALL",
            "THEME_BREAKING_NEWS",
            "THEME_ARGENTINA",
            "THEME_SERGIO_AGUERO",
            "TEAM_FC_BARCELONA"
        ],
        "url": "http://bleacherreport.com/articles/2576736-leo-messi-tells-sergio-aguero-he-has-to-wear-his-no10-shirt-while-hes-out",
        "photosUrl": "",
        "youtubeUrl": "",
        "modified": "1444373945",
        "mainTag": "TYPE_ARTICLE",
        "color": "#f94500",
        "hashtag": "Article",
        "captionsUrl": "",
        "players": []
    },
    {
        "id": "84876",
        "user_id": "8003",
        "postedby": "Admin Administrator",
        "publishDate": "2015-10-07T10:56:43+3:00",
        "photoUrl": "http://img.bleacherreport.net/img/images/photos/003/531/018/hi-res-718c63db2f32226a5917bccd633c9e7c_crop_north.jpg?w=650&h=440&q=75",
        "description": "",
        "title": "Arsenal Transfer News: Sergi Samper Open to Gunners Move, Latest Rumours",
        "addedOn": "2015-10-09T09:59:01+3:00",
        "topic_rating": "0",
        "topic_rate_num": "0",
        "tags": [
            "TOPIC_SOCCER",
            "TYPE_ARTICLE",
            "LEAGUE_EPL",
            "THEME_BREAKING_NEWS",
            "THEME_TRANSFER_WINDOW",
            "THEME_ARTUR_DAVTYAN",
            "THEME_SERGE_DEBLE",
            "THEME_MARIN_LEOVAC",
            "TEAM_FC_BARCELONA"
        ],
        "url": "http://bleacherreport.com/articles/2576694-arsenal-transfer-news-sergi-samper-open-to-gunners-move-latest-rumours",
        "photosUrl": "",
        "youtubeUrl": "",
        "modified": "1444373945",
        "mainTag": "TYPE_ARTICLE",
        "color": "#f94500",
        "hashtag": "Article",
        "captionsUrl": "",
        "players": []
    },
    {
        "id": "81954",
        "user_id": "8003",
        "postedby": "Admin Administrator",
        "publishDate": "2015-10-06T09:37:39+3:00",
        "photoUrl": "http://img.bleacherreport.net/img/images/photos/003/530/559/hi-res-5d09eb502460cc7ce08b39a0194ef20e_crop_north.jpg?w=650&h=440&q=75",
        "description": "",
        "title": "Liverpool Transfer News: Philippe Coutinho Reportedly Open to Barcelona Move",
        "addedOn": "2015-10-09T09:59:01+3:00",
        "topic_rating": "0",
        "topic_rate_num": "0",
        "tags": [
            "TOPIC_SOCCER",
            "TYPE_ARTICLE",
            "LEAGUE_EPL",
            "THEME_BREAKING_NEWS",
            "THEME_TRANSFER_WINDOW",
            "THEME_PHILIPPE_COUTINHO",
            "THEME_ARTUR_DAVTYAN",
            "THEME_SERGE_DEBLE",
            "THEME_MARIN_LEOVAC",
            "TEAM_FC_BARCELONA"
        ],
        "url": "http://bleacherreport.com/articles/2576278-liverpool-transfer-news-philippe-coutinho-reportedly-open-to-barcelona-move",
        "photosUrl": "",
        "youtubeUrl": "",
        "modified": "1444373945",
        "mainTag": "TYPE_ARTICLE",
        "color": "#f94500",
        "hashtag": "Article",
        "captionsUrl": "",
        "players": []
    },
    {
        "id": "81955",
        "user_id": "8003",
        "postedby": "Admin Administrator",
        "publishDate": "2015-10-06T09:23:13+3:00",
        "photoUrl": "http://img.bleacherreport.net/img/images/photos/003/530/558/hi-res-d3cda8934b889c7b7a7e16e31e36887d_crop_north.jpg?w=650&h=440&q=75",
        "description": "",
        "title": "Manchester United Transfer News: Neymar Reportedly Stalls on Barcelona Contract",
        "addedOn": "2015-10-09T09:59:01+3:00",
        "topic_rating": "0",
        "topic_rate_num": "0",
        "tags": [
            "TOPIC_SOCCER",
            "TYPE_ARTICLE",
            "LEAGUE_EPL",
            "THEME_BREAKING_NEWS",
            "THEME_TRANSFER_WINDOW",
            "THEME_ARTUR_DAVTYAN",
            "THEME_SERGE_DEBLE",
            "THEME_MARIN_LEOVAC",
            "TEAM_FC_BARCELONA"
        ],
        "url": "http://bleacherreport.com/articles/2576275-manchester-united-transfer-news-neymar-reportedly-stalls-on-barcelona-contract",
        "photosUrl": "",
        "youtubeUrl": "",
        "modified": "1444373945",
        "mainTag": "TYPE_ARTICLE",
        "color": "#f94500",
        "hashtag": "Article",
        "captionsUrl": "",
        "players": []
    },
    {
        "id": "80305",
        "user_id": "8003",
        "postedby": "Admin Administrator",
        "publishDate": "2015-10-05T17:58:34+3:00",
        "photoUrl": "http://img.bleacherreport.net/img/images/photos/003/530/304/hi-res-8fc4faa33377f2bdf325d6c8c5afdf3e_crop_north.jpg?w=650&h=440&q=75",
        "description": "",
        "title": "Manchester United Transfer News: Latest Shkodran Mustafi, James Wilson Rumours",
        "addedOn": "2015-10-09T09:59:01+3:00",
        "topic_rating": "0",
        "topic_rate_num": "0",
        "tags": [
            "TOPIC_SOCCER",
            "TYPE_ARTICLE",
            "LEAGUE_EPL",
            "TEAM_MANCHESTER_UNITED",
            "THEME_BREAKING_NEWS",
            "THEME_TRANSFER_WINDOW",
            "THEME_ARTUR_DAVTYAN",
            "THEME_SERGE_DEBLE",
            "THEME_MARIN_LEOVAC",
            "TEAM_REAL_MADRID"
        ],
        "url": "http://bleacherreport.com/articles/2575994-manchester-united-transfer-news-latest-shkodran-mustafi-james-wilson-rumours",
        "photosUrl": "",
        "youtubeUrl": "",
        "modified": "1444373947",
        "mainTag": "TYPE_ARTICLE",
        "color": "#f94500",
        "hashtag": "Article",
        "captionsUrl": "",
        "players": []
    },
    {
        "id": "79129",
        "user_id": "8003",
        "postedby": "Admin Administrator",
        "publishDate": "2015-10-04T22:12:42+3:00",
        "photoUrl": "http://img.bleacherreport.net/img/images/photos/003/529/933/hi-res-a3326d599e5ed1e926201155635f2603_crop_north.jpg?w=650&h=440&q=75",
        "description": "",
        "title": "Arsenal Transfer News: Gunners Racing to Beat Barca as Nolito Rumours Emerge",
        "addedOn": "2015-10-09T09:59:01+3:00",
        "topic_rating": "0",
        "topic_rate_num": "0",
        "tags": [
            "TOPIC_SOCCER",
            "TYPE_ARTICLE",
            "LEAGUE_EPL",
            "TEAM_ARSENAL",
            "THEME_BREAKING_NEWS",
            "THEME_TRANSFER_WINDOW",
            "THEME_ARTUR_DAVTYAN",
            "THEME_SERGE_DEBLE",
            "THEME_MARIN_LEOVAC",
            "TEAM_FC_BARCELONA"
        ],
        "url": "http://bleacherreport.com/articles/2575668-arsenal-transfer-news-gunners-racing-to-beat-barca-as-nolito-rumours-emerge",
        "photosUrl": "",
        "youtubeUrl": "",
        "modified": "1444373946",
        "mainTag": "TYPE_ARTICLE",
        "color": "#f94500",
        "hashtag": "Article",
        "captionsUrl": "",
        "players": []
    },
    {
        "id": "77815",
        "user_id": "8003",
        "postedby": "Admin Administrator",
        "publishDate": "2015-10-04T12:00:50+3:00",
        "photoUrl": "http://img.bleacherreport.net/img/images/photos/003/529/695/hi-res-e26e40db50d6f7b354782f4eecb4145c_crop_north.jpg?w=650&h=440&q=75",
        "description": "",
        "title": "Liverpool Transfer News: Philippe Coutinho Reacts to Neymar's Barcelona Comments",
        "addedOn": "2015-10-09T09:59:01+3:00",
        "topic_rating": "0",
        "topic_rate_num": "0",
        "tags": [
            "TOPIC_SOCCER",
            "TYPE_ARTICLE",
            "LEAGUE_EPL",
            "THEME_BREAKING_NEWS",
            "THEME_TRANSFER_WINDOW",
            "THEME_ARTUR_DAVTYAN",
            "THEME_SERGE_DEBLE",
            "THEME_MARIN_LEOVAC",
            "TEAM_FC_BARCELONA"
        ],
        "url": "http://bleacherreport.com/articles/2575431-liverpool-transfer-news-philippe-coutinho-reacts-to-neymars-barcelona-comments",
        "photosUrl": "",
        "youtubeUrl": "",
        "modified": "1444373946",
        "mainTag": "TYPE_ARTICLE",
        "color": "#f94500",
        "hashtag": "Article",
        "captionsUrl": "",
        "players": []
    },
    {
        "id": "73005",
        "user_id": "8003",
        "postedby": "Admin Administrator",
        "publishDate": "2015-10-02T06:56:27+3:00",
        "photoUrl": "http://img.bleacherreport.net/img/images/photos/003/528/842/hi-res-afb33e87d9e9eb8e843b12cb197997ec_crop_north.jpg?w=650&h=440&q=75",
        "description": "",
        "title": "Manchester United Transfer News: Toni Kroos Targeted Amid Real Madrid Rumours",
        "addedOn": "2015-10-09T09:59:01+3:00",
        "topic_rating": "0",
        "topic_rate_num": "0",
        "tags": [
            "TOPIC_SOCCER",
            "TYPE_ARTICLE",
            "LEAGUE_EPL",
            "THEME_BREAKING_NEWS",
            "THEME_TRANSFER_WINDOW",
            "THEME_ARTUR_DAVTYAN",
            "THEME_SERGE_DEBLE",
            "THEME_MARIN_LEOVAC",
            "TEAM_REAL_MADRID"
        ],
        "url": "http://bleacherreport.com/articles/2574738-manchester-united-transfer-news-toni-kroos-targeted-amid-real-madrid-rumours",
        "photosUrl": "",
        "youtubeUrl": "",
        "modified": "1444373947",
        "mainTag": "TYPE_ARTICLE",
        "color": "#f94500",
        "hashtag": "Article",
        "captionsUrl": "",
        "players": []
    }
],
}

【问题讨论】:

    标签: php mysql json error-handling


    【解决方案1】:

    注意:未定义变量:tagscolor in C:\xampp\htdocs\jsonreader\index.php 在第 64 行

    在您的 SQL 查询中,您有一个变量 $tagscolor,但您没有在 sql 语句之前的代码中设置此变量。

    注意:未定义的变量:tagsmodified in C:\xampp\htdocs\jsonreader\index.php 在第 64 行

    在您的 SQL 查询中,您有一个变量 $tagsmodified,但您没有在 sql 语句之前的代码中设置此变量。

    注意:数组到字符串的转换 C:\xampp\htdocs\jsonreader\index.php 第 66 行错误:添加问题 进入 DB 您的 SQL 语法有错误;检查手册 对应于您的 MySQL 服务器版本,以便使用正确的语法 在第 1 行的 'order,description) VALUES('e','','','','','')' 附近

    变量$players是一个数组,看你的json代码("players": []),你可以字符串化$players或者json_encode

    $players = json_encode($players);
    

    顺便说一句:有什么用:

    $players = $data['topics']['players']; 
    $players=$data['players'];
    

    您覆盖了$players .. 并且$data['players'] 不存在。 在提交关于 SO 的问题之前,请仔细检查您自己的代码,它充满了错误。

    【讨论】:

    • 我删除了未定义的变量错误,并且无法字符串化。
    • $players = $data['topics']['players']; 'players' 是与 $players=$data['players'] 不同表上的节点;
    • 可能是变量名相同,第一个语句根本没有这样的功能。
    • 我是 JSON 新手,请耐心等待
    【解决方案2】:

    除了已经突出显示的问题之外,您的 JSON 无效。例如,将您的数据粘贴到http://jsonlint.com/ 并检查它。删除最后的逗号应该可以解决这个问题。

    一旦您的 JSON 有效,您应该会发现您的 $data 变量不再为空:

    var_dump($data);
    

    您需要修改代码以循环遍历它以处理所有内容(例如,有 18 个“标签”数组)。

    【讨论】:

    • 我认为这是我在此处粘贴 Json 数据时遇到的问题,在我这边没问题。我能够添加 foreach 循环,并且实际上插入了 2 个表。只有 hashtags 表不会带来 mysql 错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 2012-09-24
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    相关资源
    最近更新 更多