如何将json对象插入mysql表

时间:2017-11-08 00:26:42

标签: php mysql json mysqli

有许多示例可以解析JSON,然后将相应的字段插入MySQL表。

我的情况与我在运行时创建json的方式不同。

我的表看起来像这样:

mysql> describe turkers_data;
+-----------+----------+------+-----+---------+-------+
| Field     | Type     | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+-------+
| id        | char(36) | NO   | PRI | NULL    |       |
| sentences | json     | NO   |     | NULL    |       |
+-----------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

根据收到的输入,我在json中使用json_encode方法构建php,我在jsonlint上验证了它,当然它有效。

示例json:

{
    "opening": "[\"John arrived at Sally's house to pick her up.\",\"John and Sally were going to a fancy restaurant that evening for a dinner.\",\"John was little nervous because he was going to ask Sally to marry him.\"]",
    "first_part": "[\"aa\",\"bb\"]",
    "first_mid": "[\"Waiter shows John and Sally to their table.\"]",
    "mid_part": "[\"cc\",\"dd\"]",
    "mid_late": "[\"John asks Sally, \\\"Will you marry me?\\\"\"]",
    "last_part": "[\"ee\",\"ff\",\"gg\"]"
}

我使用以下代码使用mysqli

插入到mysql表中
$opening = array("John arrived at Sally's house to pick her up.", "John and Sally were going to a fancy restaurant that evening for a dinner.", "John was little nervous because he was going to ask Sally to marry him.");
$mid_early = array("Waiter shows John and Sally to their table.");
$mid_late = array('John asks Sally, "Will you marry me?"');
$json_data->opening = json_encode($opening);
$json_data->first_part = json_encode($jSentence_1);
$json_data->first_mid = json_encode($mid_early);
$json_data->mid_part = json_encode($jSentence_2);
$json_data->mid_late = json_encode($mid_late);
$json_data->last_part = json_encode($jSentence_3);

$data = json_encode($json_data);
echo($data);


$sql = "INSERT INTO turkers_data (id, sentences)
VALUES ($id, $data)";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

但它不起作用,我收到错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"opening":"[\"John arrived at Sally's house to pick her up.\",\"John and Sally w' at line 2

我不知道出了什么问题。我找不到有关如何执行此操作的更多信息,我读到不建议将json数据转储到mysql表中,但在我的情况下,我不确定有多少句子会去那里。另外,我认为这暂时有用,我打算从JSON获取mysql,然后处理python中的数据。

请原谅我使用jsonJSONMySQLmysql,我还不知道标准。

1 个答案:

答案 0 :(得分:1)

您的SQL插入有问题,因为您有:

$sql = "INSERT INTO turkers_data (id, sentences) VALUES ($id, $data)";

$data上的引号没有转义,$data也没有用单引号括起来。

你应该将它构建为一个准备好的语句并绑定params,它将为你完成所有这些:

$sql = "INSERT INTO turkers_data (id, sentences) VALUES (?,?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param('ss', $id, $data );
$stmt->execute();

以上假设您使用的是mysqli,而不是PDO。如果是PDO,这是PDO方法的语法:

$sql = "INSERT INTO turkers_data (id, sentences) VALUES (?,?)";
$stmt = $conn->prepare($sql);
$stmt->execute(array($id, $data));

修改

最后的努力(和ILL-ADVISED),如果你的php和mysql不支持预处理语句(它应该!),那么你可以采用旧的方法来包装和转义sql构建字符串中的字段:< / p>

$sql = "INSERT INTO turkers_data (id, sentences) 
        VALUES (
               '". $conn->real_escape_string($id) ."',
               '". $conn->real_escape_string($data) ."'
               )";

但这不建议!如果不惜一切代价 ,您应该尝试使用预先准备好的语句 ,或升级您的PHP或mysqli扩展程序。