gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:29:53 25 4
gpt4 key购买 nike

这是我尝试加载到 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”例如酒精度、噪音水平。在“atributos simples”中,我创建了 2 个字段,名称和值,将酒精转化为名称,并完全禁止值。我的疑问是我如何解析数据,以便可以将酒精和这个“atributos simples”加载到 mysql 表“Atributos Simples”中的字段 Designation。

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

最佳答案

看看我能不能帮上忙。首先,我会将您当前的代码更改为以下内容:

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

这应该可以让您到达或到达您想去的地方。

关于php - 在 mysql 的特定表中插入 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31730450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com