如何将 JSON 数组插入到 Mysql 中? [英] How to insert a JSON Array into Mysql?

查看:78
本文介绍了如何将 JSON 数组插入到 Mysql 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找解决问题的示例,但我很困惑.如果我有一个这样的 json 数组:

I look for examples to solve my problem but im confused. If Ii have a json array like this:

$json = '{"a":"test","b":"test2"},{"a":"test3","b":"test4"}';

//decode to get as php variable
$obj = json_decode($json);

我的问题是当我尝试在数据库中插入值

My problem is when i try to insert the values in the database

mysql_query("INSERT INTO suspiciousactivity (ID,Notes)
VALUES ('".$obj->{'a'}."','".$obj->{'b'}."')")or die(mysql_error());

我收到此错误:重复条目 PRIMARY

如何将多个值从我的 JsonArray 插入到我的数据库中?

How can I insert a multiple values from my JsonArray to my database?

推荐答案

尝试使用:

$json = '{"a":"test","b":"test2"},{"a":"test3","b":"test4"}';

//decode to get as php variable
$arr = json_decode($json,true); //true to decode as a array not an object

mysql_query("INSERT INTO suspiciousactivity (ID,Notes)
VALUES ('".$arr[0]['a']."','".$arr[0]['b']."')")or die(mysql_error());
//use it as an array.

这篇关于如何将 JSON 数组插入到 Mysql 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆