如何将json数组插入到具有多个记录的mysql数据库中,如db image

时间:2017-04-15 07:17:26

标签: php mysql arrays json

我有这个json数组,它会在mysql表中插入给定的图像:

My Sql table image

[{"F_relation":"Son","F_name":"fhj","F_address_type":"Other","F_address":"55556","F_phone":"556"}, {"F_relation":"Husband","F_name":"fhj","F_address_type":"Other","F_address":"5566","F_phone":"889"}, {"F_relation":"Husband","F_name":"fhj","F_address_type":"Same","F_address":"ak, USA, 5566","F_phone":"89888789"}]

2 个答案:

答案 0 :(得分:1)

使用json_decode将json字符串转换为数组。

$json='[{"F_relation":"Son","F_name":"fhj","F_address_type":"Other","F_address":"55556","F_phone":"556"},{"F_relation":"Husband","F_name":"fhj","F_address_type":"Other","F_address":"5566","F_phone":"889"},{"F_relation":"Husband","F_name":"fhj","F_address_type":"Same","F_address":"ak, USA, 5566","F_phone":"89888789"}]';
//store the  json string in a variable 

$a=json_decode($json,true); //will convert json string to array
$len=sizeof($a);
for($i=0;$i<$len;$i++){
    $F_relation=$a[$i]['F_relation']; //retrieve values 
    $F_name=$a[$i]['F_name'];
    $F_address_type=$a[$i]['F_address_type'];
    $F_address=$a[$i]['F_address'];
    $F_phone=$a[$i]['F_phone'];

    //write your query to insert, perform other operation 
}

答案 1 :(得分:0)

var res = jQuery.parseJSON(response.responseText);
var result = res.d;

因此,您可以将其用作数组:result[0].F_relation