2015-10-18 38 views
-2

我已經嘗試了很多網站,以找到如何在MySQL中插入JSON,但我沒有找到任何適當的解決方案。你能幫我插入mysql表中的波紋管json,並告訴我表中列的必需名稱。如何將JSON插入到MySQL表與PHP

{ 
    "request" : ` { 
     "Target" : "Affiliate_Report", 
     "Format" : "json", 
     "Service" : "HasOffers", 
     "Version" : "2", 
     "NetworkId" : "network_id", 
     "Method" : "getConversions", 
     "api_key" : "api_key", 
     "fields" : ["Stat.affiliate_info1", "Offer.name"], 
     "limit" : "1" 
    }, 
    "response" : { 
     "status" : 1, 
     "httpStatus" : 200, 
     "data" : { 
      "page" : 1, 
      "current" : 1, 
      "count" : 82, 
      "pageCount" : 82, 
      "data" : [{ 
        "Stat" : { 
         "affiliate_info1" : "aashiq" 
        }, 
        "Offer" : { 
         "name" : "App Download - Paytm - Android - IN - Incent" 
        } 
       } 
      ], 
      "dbSource" : "branddb" 
     }, 
     "errors" : [], 
     "errorMessage" : null 
    } 
} 

我需要插入數據:aashiq, App Download - Paytm - Android - IN - Incent從上面的JSON

+0

給我們一些更多背景,例如您是否使用MySQL控制檯進行操作,使用編程語言等。至於列名完全取決於您定義名稱,我不相信有人可以幫助您。 –

+0

您是否正在使用hasoffers API。 ..我可以問你什麼時候開始使用API​​ ...你想用API來做什麼? –

回答

0

我會給一個嘗試。忍受我導致我的PHP有點生疏......所以,我們可以說$mydata是你存儲JSON的數組對象。然後:

$data = $mydata["response"]["data"]; 
$name = $data[0]["Stat"]["affiliate_info1"]; // will store aashiq 
$offer = $data[0]["Offer"]["name"]; // will store App Download etc... 

創建一個與表offer和三個字段:offers數據庫MySQL數據庫:一個ID將被自動遞增,affiliate VARCHARfinal_offer VARCHAR

//copying from http://www.kodingmadesimple.com/2014/12/how-to-insert-json-data-into-mysql-php.html?m=1 
//connect to mysql db 
$con = mysql_connect("localhost","username","password") or die('Could not connect: ' . mysql_error()); 
//connect to the offers database 
mysql_select_db("offers", $con); 
//write your Insert query, you omit the autoincrement field and 
//after the keyword VALUES you use your variables enclosed in '' 
$sql = "INSERT INTO offer(affiliate, final_offer) VALUES('$name', '$offer'"); 
if(!mysql_query($sql,$con)) 
{ 
    die('Error : ' . mysql_error()); 
} 

我相信上面的代碼更或更少正確