How to generate mysql query with this json data?
This is the sample data receiving to the php script
[{"id":1,"due_date":"2011-09-03","due_amount":"48279.00","wo_interest":"45980.00"},
{"id":2,"due_date":"2011-10-03","due_amount":"482开发者_运维问答79.00","wo_interest":"45980.00"}]
and the table fields are in this order
loan_no,i_id,due_date,installment,due_amount,wo_interest
below the order which matching to json data im receiving.
i_id,due_date,due_amount,wo_interest
how to add loan_no and installment to this and make a mysql query with it?
$array = json_decode( $sample_data_recieving );
foreach ( $array as $row ){
$row['loan_no'] = 'SOMETHING_YOU_WANT';
$row['installment'] = 'SOMETHING_YOU_WANT';
mysql_query("
INSERT INTO tbl (i_id,due_date,due_amount,wo_interest,loan_no,installment)
VALUES ('" . implode( "','", $row ) . "')" );
}
You can do it through the following steps:
- Make an array out of the JSON data, any PHP JSON decoder can do that.
- Merge this array with the values you also want to set like
$res = array_merge($json_array, array( 'loan_no' => 12);
- Create the MYSQL Query INSERT with the $res array.
Order doesn't matter for the MYSQL INSERT.
you have to
$arr = json_decode($json,true);
loop through it, write your query, the order does not matter in this case
精彩评论