所以我有一个json文件如何在mysql中插入它?

时间:2015-07-10 23:27:25

标签: php mysql json

我希望制作一个php脚本,每小时从json文件更新一次mysql。

api就是那个

http://backpack.tf/api/IGetMarketPrices/v1/?key=51f7eb704bd7b8231900000c&appid=730&format=json

我如何从json中复制这些东西并将它们放入mysql中?

我的意思是那样的

"AK-47 | Aquamarine Revenge (Battle-Scarred)": {
    "last_updated": 1436569230,
    "quantity": 34,
    "value": 2268
},

"AK-47 | Aquamarine Revenge (Factory New)": {
    "last_updated": 1436569230,
    "quantity": 21,
    "value": 9386
},
"AK-47 | Aquamarine Revenge (Field-Tested)": {
    "last_updated": 1436569230,
    "quantity": 55,
    "value": 4968
},
"AK-47 | Aquamarine Revenge (Minimal Wear)": {
    "last_updated": 1436569230,
    "quantity": 40,
    "value": 6018
},
"AK-47 | Aquamarine Revenge (Well-Worn)": {
    "last_updated": 1436569230,
    "quantity": 40,
    "value": 3597
},
"AK-47 | Black Laminate (Battle-Scarred)": {
    "last_updated": 1436569230,
    "quantity": 50,
    "value": 345
},
"AK-47 | Black Laminate (Factory New)": {
    "last_updated": 1436569230,
    "quantity": 8,
    "value": 8593
},
"AK-47 | Black Laminate (Field-Tested)": {
    "last_updated": 1436569230,
    "quantity": 141,
    "value": 308
},

我想让它像mys那样进入

enter image description here

这是我到目前为止所得到的:

<?php

$json = file_get_contents('http://backpack.tf/api/IGetMarketPrices/v1/?key=51f7eb704bd7b8231900000c&appid=730&format=json');
$obj = json_decode($json,true);

//Database Connection
require_once 'db.php';

/* insert data into DB */
foreach($obj as $item) {
   mysql_query("INSERT INTO `cyberst_CSGO`.`items` ( cost, lastupdate)
   VALUES ('".$item['value']."'', '".$item['last_updated']."')");

}
//database connection close
mysql_close($con);

//}
?>

1 个答案:

答案 0 :(得分:0)

编辑:此代码生成临时制表符分隔文件,并使用LOAD DATA IN FILE将该文件插入数据库,然后删除临时文件。

  1. 使用MySQLi - 现在在PHP中不推荐使用mysql函数
  2. 编辑:cost = value * 0.01
  3. 将一堆数据加载到MySQL的最快方法是使用LOAD DATA INFILE
  4. 我没有测试数据库导入,但我已经多次使用过这种方法,而且效果很好。
  5. 您可能需要先使用$name $cost $rowmysqli_real_escape_string()转义$conn->real_escape_string()。我没有检查你的所有数据。
  6. 这是PHP:

    $json = file_get_contents('http://backpack.tf/api/IGetMarketPrices/v1/?key=51f7eb704bd7b8231900000c&appid=730&format=json');
    $obj = json_decode($json,true);
    
    //open a temporary file to hold data
    $path  = str_replace('\\','/',realpath(dirname(__FILE__)));
    $filename = time().'tmp.csv';
    $filename = $path.'/'.$filename;
    $tmpfile = fopen($filename,'w+');
    
    //data to loop through and write to temporary file
    $loopme = $obj['response']['items'];
    
    foreach($loopme as $name=>$row) {
        //i'm guessing this is what cost is
        $cost = $row['value'] * 0.01;
        //write to our tmp file
        fwrite($tmpfile,"$name\t$cost\t$row[last_updated]\n");
    }
    
    // this is the fastest way to load large amounts of data into MySQL
    $sql = "LOAD DATA INFILE '$filename'
      INTO TABLE `cyberst_CSGO`.`items`
      FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n'";
    
    //run your mysql query
    
    //delete temporary file
    unlink($filename);