【问题标题】:How to insert json object into mysql table如何将json对象插入mysql表
【发布时间】:2018-04-20 13:04:50
【问题描述】:

有很多例子可以解析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)

根据收到的输入,我在php 中使用json_encode 方法构建了一个json,我已经在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 表中,但在我的情况下,我不确定有多少句子要去那里.另外,我相信这暂时可以达到目的,我打算从mysql取回JSON并处理python中的数据。

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

【问题讨论】:

    标签: php mysql json mysqli


    【解决方案1】:

    你的 SQL 插入有问题,因为你有这个:

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

    $data 上的引号没有转义,$data 也没有用单引号包裹。

    您应该将其构建为准备好的语句并绑定将为您完成所有这些工作的参数:

    $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));
    

    编辑

    最后的努力(并且不建议),如果您的 php 和 mysql 不支持准备好的语句(它应该!),那么您可以诉诸在 sql 构建字符串中包装和转义字段的旧方法:

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

    但不建议这样做!如果不惜一切代价您应该尝试让准备好的语句工作,或者升级您的 PHP 或 mysqli 扩展。

    【讨论】:

    • 感谢您的回答。我认为它是 PDO,我在这里遵循了 w3schools 的教程:w3schools.com/php/php_mysql_insert.asp 如果我将代码更改为您建议的内容,我会收到错误 Error: INSERT INTO turkers_data (id, sentences) VALUES (?, ?) 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 '?, ?)' at line 1
    • 这让我觉得你使用的是标准的mysql 方法(不是mysqli,请注意'i')...?你能确认一下吗?
    • $conn = new mysqli($servername, $username, $password, $dbname);,确实是mysqli
    • 好的,那么它不是 PDO...mysqli 应该使用 ?, ?在里面......这就是准备好的陈述的全部内容:D
    • 啊!很高兴知道。祝你好运,玩得开心编码:)
    猜你喜欢
    • 1970-01-01
    • 2020-08-23
    • 2020-11-19
    • 2016-05-04
    • 2018-06-19
    • 2014-05-24
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    相关资源
    最近更新 更多