【问题标题】:Insert JSON file in specific tables in mysql在mysql的特定表中插入JSON文件
【发布时间】:2015-07-30 17:33:45
【问题描述】:

这是我尝试加载到 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 个表,我将其命名为“Atributos complexos”,这些表在内部具有很多属性,例如 Music 和 Ambience,以及“Atributos Simples”例如酒精、噪音水平。在“attributos simples”中,我创建了 2 个字段,名称和值,这将转化为酒精是名称和完整的值。 我的疑问是我如何解析数据,以便可以将酒精和这个“atributos simples”加载到 mysql 中表“Atributos Simples”中的字段 Designation 中。

谁能帮帮我,这真的很重要

【问题讨论】:

  • 您正在使用 PDO 准备好的语句,这很好,但不要忘记使用 placeholder values 而不是使用字符串插值。这段代码充满了SQL injection bugs,因为没有任何东西被转义。

标签: php mysql json


【解决方案1】:

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

<?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();
      }
}

这应该可以让您了解您想去的地方。

【讨论】:

    猜你喜欢
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 2015-12-25
    相关资源
    最近更新 更多