2011-08-03 32 views
0

這是採樣數據接收到PHP腳本如何用這個json數據生成mysql查詢?

[{"id":1,"due_date":"2011-09-03","due_amount":"48279.00","wo_interest":"45980.00"}, 
{"id":2,"due_date":"2011-10-03","due_amount":"48279.00","wo_interest":"45980.00"}] 

和表中的字段是在該順序​​

loan_no,i_id,due_date,installment,due_amount,wo_interest 

順序低於該匹配到JSON數據即時接收。

i_id,due_date,due_amount,wo_interest 

如何將loan_no和installment添加到此並使用它進行mysql查詢?

+1

順序無關緊要 - 既不在JSON對象中,也不在SQL查詢的列名中。 – phihag

+0

從JSON解碼到數組,對數組進行排序,添加需要的內容,循環並每次執行查詢。 – daGrevis

+0

然後不要對它進行分類。 :D – daGrevis

回答

0

您可以通過以下步驟來做到:

  • 製作一個數組超出了JSON數據,任何PHP JSON解碼器都可以做到這一點。
  • 將此數組合併爲您想要設置的值,如$res = array_merge($json_array, array('loan_no' => 12);
  • 使用$ res數組創建MYSQL查詢插入。

訂單對於MYSQL INSERT無關緊要。

+0

非常感謝。不知道插入順序是無關緊要的 –

0

,你必須通過它

$arr = json_decode($json,true); 

循環,寫你的查詢中,爲了在這種情況下並不重要

1
$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) . "')"); 
}