在mysql中的特定表中插入JSON文件

时间:2015-07-30 17:33:45

标签: php mysql json

这是我尝试加载到mysql的JSON

{
    "business_id": "fNGIbpazjTRdXgwRY_NIXA",
    "full_address": "1201 Washington Ave\nCarnegie, PA 15106",
    "hours": {

    },
    "open": true,
    "categories": ["Bars",
                   "American (Traditional)",
                   "Nightlife",
                   "Lounges",
                   "Restaurants"],
    "city": "Carnegie",
    "review_count": 5,
    "name": "Rocky's Lounge",
    "neighborhoods": [],
    "longitude": -80.084941599999993,
    "state": "PA",
    "stars": 4.0,
    "latitude": 40.396468800000001,
    "attributes": {
        "Alcohol": "full_bar",
        "Noise Level": "average",
        "Music": {
            "dj": false,
            "background_music": true,
            "karaoke": false,
            "live": false,
            "video": false,
            "jukebox": false
        },
        "Attire": "casual",
        "Ambience": {
            "romantic": false,
            "intimate": false,
            "touristy": false,
            "hipster": false,
            "divey": false,
            "classy": false,
            "trendy": false,
            "upscale": false,
            "casual": false
        },
        "Good for Kids": true,
        "Wheelchair Accessible": false,
        "Good For Dancing": false,
        "Delivery": false,
        "Coat Check": false,
        "Smoking": "no",
        "Accepts Credit Cards": true,
        "Take-out": false,
        "Price Range": 2,
        "Outdoor Seating": false,
        "Takes Reservations": false,
        "Waiter Service": true,
        "Caters": false,
        "Good For": {
            "dessert": false,
            "latenight": false,
            "lunch": false,
            "dinner": false,
            "brunch": false,
            "breakfast": false
        },
        "Parking": {
            "garage": false,
            "street": false,
            "validated": false,
            "lot": false,
            "valet": false
        },
        "Has TV": true,
        "Good For Groups": true
    },
    "type": "business"
}

我正在使用以下代码

<?php
   try {
    $conn = new PDO("mysql:host=localhost:118;dbname=mydb", "root", "1234");
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }

    //read the json file contents
    $jsondata = file_get_contents('c:\yelp_academic_dataset_business.json');

    ini_set('memory_limit', '512M');
    //convert json object to php associative array
    $data = json_decode($jsondata, true);

    //get the employee details
    $idBusiness = $data['business_id'];
    $name = $data['name'];
    $neighborhoods = $data['neighborhoods'];
    $full_address = $data['full_address'];
    $city = $data['city'];
    $state = $data['state'];
    $latitude = $data['latitude'];
    $longitude = $data['longitude'];
    $stars = $data['stars'];
    $review_count = $data['review_count'];
    $open = $data['open'];
 $procedure = $conn -> prepare("INSERT INTO business(business_id, name, neighborhoods, full_address, city, state, latitude, longitude, stars, review_count, open)
    VALUES('$idBusiness', '$name', '$neighborhoods', '$full_address', '$city', '$state', '$latitude', '$longitude', '$stars', '$review_count', '$open')");
   $procedure -> execute(); ?>

脚本运行但未插入数据库,不知道发生了什么。

然后我有另一个问题。

例如,对于属性,我无法以这种方式加载到mysql,因此我需要创建2个名为&#34; Atributos complexos&#34;那些在音乐和氛围中有很多属性的人,以及&#34; Atributos Simples&#34;如酒精,噪音水平。在&#34; atributos simples&#34;我创造了2个fiels,名称和价值,这意味着酒精是指定和全价值。 我怀疑的是我如何解析数据,以便酒精和这个简单的&#34; atributos简单&#34;可以加载到表格中的指定&#34; Atributos Simples&#34;在mysql中。

任何人都可以帮助我,非常重要

1 个答案:

答案 0 :(得分:0)

让我们看看我是否可以提供帮助。首先,我会将您当前的代码更改为以下内容:

<?php
//convert json object to php associative array
$data = json_decode($jsondata, true);

$procedure = $conn->prepare("INSERT INTO business (business_id, name, neighborhoods, full_address, city, state, latitude, longitude, stars, review_count, open)
VALUES (:business_id, :name, :neighborhoods, :full_address, :city, :state, :latitude, :longitude, :stars, :review_count, :open)");

$procedure->bindParam(':business_id', $data['business_id']);
$procedure->bindParam(':name', $data['name']);
$procedure->bindParam(':neighborhoods', $data['neighborhoods']);
$procedure->bindParam(':full_address', $data['full_address']);
$procedure->bindParam(':city', $data['city']);
$procedure->bindParam(':state', $data['state']);
$procedure->bindParam(':latitude', $data['latitude']);
$procedure->bindParam(':longitude', $data['longitude']);
$procedure->bindParam(':stars', $data['stars']);
$procedure->bindParam(':review_count', $data['review_count']);
$procedure->bindParam(':open', $data['open']);

$procedure->execute() or die(print_r($procedure->errorInfo(), true));
?>

die语句应该为您提供插入失败原因的一些信息。

关于第二个问题,您至少需要三个字段:business_iddesignationvalue。您需要business_id才能稍后链接到该信息。

我个人填写atributos_simples表,我将执行以下操作:

foreach($data['attributes'] as $k=>$v)
{
     if(!is_array($v))
     {
            $simple = null;

            $simple = $conn->prepare("INSERT INTO atributos_simples (business_id,designation,value) VALUES (:business_id,:designation,:value)");

            $simple->bindParam(':business_id', $data['business_id']);
            $simple->bindParam(':designation', $k);
            $simple->bindParam(':value', $v);

            $simple->execute();
      }
}

这可以让你在那里或那里找到你想去的地方。