多くの例では、JSON
を解析し、それぞれのフィールドをMySQL
テーブルに挿入します。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)
、私はalredy jsonlint
で検証し、それはもちろん有効であるphp
でjson_encode
方法を使用してjson
を構築します。
例の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の
$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();
を使用してMySQLのテーブルに挿入するには、次のコードを使用しますが、それは動作しません、私はエラーを取得:
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
にデータを処理する予定です。
json
、JSON
、MySQL
、mysql
、私はまだ標準を知らない。あなたがこれを持っているので
ありがとうございました。私はそれがPDOだと思う、私はここw3schoolsからのチュートリアルに従っている:https://www.w3schools.com/php/php_mysql_insert.asp私があなたの提案にあなたのコードを変更した場合、私はエラーが発生します:エラー:INSERT INTO turkers_data( id、sentences)VALUES(?、?) SQL構文に誤りがあります。 '?、?)' 1行目で使用するようにMySQLサーバのバージョンに対応するマニュアルを確認してください。 – Adorn
これは、あなたが標準の 'mysql'メソッドを使っていると思うようにします(' mysqli'ではなく、 'i ')...?あなたは確認できますか? – IncredibleHat
'$ conn = new mysqli($ servername、$ username、$ password、$ dbname);'それは確かに 'mysqli'です – Adorn