使用foreach将json数组插入mysql

时间:2018-02-19 21:59:56

标签: php mysql json

我有一个json数组,我不想插入我的sql数据库,但是我对php不太熟悉,所以我可能会做多个错误。

PHP:

<?php

//Runescape API Class
require_once 'assets/api/Runescape_API.class.php';
$rsapi = new Runescape_API;

//Clan Members
if($ressource = $rsapi->getClanMembers("Made+in+norway")){
    $result["getClanMembers"] = $ressource;
}


$obj = json_decode($result["getClanMembers"],true);

echo $obj;

//MYSQL Connection
require_once('assets/inc/link.php');


foreach ((array) $obj as $item) {
       mysqli_query($db, "INSERT INTO `minrs`.`members` (name, rank, xp, kills) VALUES ('".$item['name']."', '".$item['rank']."', '".$item['xp']."', '".$item['kills']."')");
    }

?>

JSON:

{
    "getClanMembers": [
     {
         "name": "Benjamin",
         "rank": "Owner",
         "xp": "488731559",
         "kills": "0"
     },
     {
         "name": "tiMMe",
         "rank": "Deputy Owner",
         "xp": "341737592",
         "kills": "0"
     },
     {
         "name": "uncel dolan",
         "rank": "Overseer",
         "xp": "551290158",
         "kills": "4"
     }
  ]
}

我目前收到的错误:

  

json_decode()期望参数1为字符串,给定数组

我尝试过多次“soloutions”,但他们只是给了我更多错误,所以这就是我开始使用的代码。

感谢我能得到的任何帮助,谢谢!

0 个答案:

没有答案