【发布时间】:2019-12-22 09:43:39
【问题描述】:
我想将 .json 数据插入 MySQL。我使用了这里的代码(Decode Json data Array and insert in to mysql),但我遇到了空值问题。
json 文件(示例)
[{'counsel_id':'2019-000005', 'big_cate':'Smartphone',
'mid_cate':'Orange', 'small_cate':'AA',
'title':'faceID doesn't work',
'question_date':'2019-08-01',
'question':'faceID doesn't work. how can i fix it?',
'answer-date':'2019-08-01',
'answer':'hello blah blah'
},
{'counsel_id':'2019-000015', 'big_cate':'Smartphone',
'mid_cate':'Star', 'small_cate':'BB',
'title':'Fingerprint Recognition doesn't work',
'question_date':'2019-08-10',
'question':'Fingerprint Recognition doesn't work. how can i fix it?',
'answer-date':'2019-08-11',
'answer':'hello blah blah'
},
{'counsel_id':'2019-000018', 'big_cate':'Smartphone',
'mid_cate':'Orange', 'small_cate':'AA',
'title':'The screen broken',
'question_date':'2019-08-16',
'question':'How much will it cost to fix it??'
}]
MySQL 表
CREATE TABLE IF NOT EXISTS `counsel` (
`id` INT NOT NULL AUTO_INCREMENT,
`counsel_id` CHAR(13) NOT NULL,
`big_cate` VARCHAR(15) NOT NULL,
`mid_cate` VARCHAR(15) NOT NULL,
`small_cate` VARCHAR(15) NOT NULL,
`title` VARCHAR(45) NOT NULL,
`question_date` DATETIME NOT NULL,
`question` TEXT NOT NULL,
`answer_date` DATETIME NULL,
`answer` TEXT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
php代码
<?php
$jsonFile="final_03.json";
$jsondata = file_get_contents($jsonFile);
$data = json_decode($jsondata, true);
$servername = "localhost";
$username = "root";
$password = "mypassword";
$dbname = "myDBname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
foreach ($data as $row) {
$sql = "INSERT INTO counsel (counsel_id, big_cate, mid_cate, small_cate,
title, question_date, question)
VALUES ('" . $row["receiptNum"] . "', '" . $row["gooboon"] . "',
'" . $row["itemCode"] . "','" . $row["item"] . "',
'" . $row["title"] . "','" . $row["date"] . "',
'" . $row["question"] . "')";
$conn->query($sql);
}
$conn->close();
?>
我想将所有 json 数据插入 MySQL,但是当 json 文件没有 'answer-date'、'answer' 时出现错误。如果json列表没有'answer-date'、'answer',我想添加一个空值。谁能帮帮我?
(+) 我用函数解决了 (array_key_exists())
if (array_key_exists('answerDate', $row)) {
$sql = "INSERT INTO counsel (counsel_id, big_cate, mid_cate, small_cate, title, question_date, question, answer_date, answer) VALUES ('" . $row["receiptNum"] . "', '" . $row["gooboon"] . "','" . $row["itemCode"] . "','" . $row["item"] . "','" . $row["title"] . "','" . $row["date"] . "','" . $row["question"] . "', '" . $row["answerDate"] . "','" . $row["answer"] . "')";
echo "answer exists!";
} else {
$sql = "INSERT INTO counsel (counsel_id, big_cate, mid_cate, small_cate, title, question_date, question) VALUES ('" . $row["receiptNum"] . "', '" . $row["gooboon"] . "','" . $row["itemCode"] . "','" . $row["item"] . "','" . $row["title"] . "','" . $row["date"] . "','" . $row["question"] . "')";
echo "answer doesn't exists!";
}
$conn->query($sql);
【问题讨论】: