PHP解码JSON数组并插入MYSQL数据库

时间:2015-04-24 14:21:17

标签: javascript php mysql arrays json

我正在尝试创建一个解码JSON数组并将其插入数据库的PHP脚本。到目前为止,我已经设法让脚本插入数组中的第一行而没有别的。

我需要添加什么才能让脚本插入数组中的所有行?

这是数组,忽略“列表”,我还不需要那些数据(它非常大): json 这是脚本:

<?php
$servername = "localhost";
$username = "root";
$password = "";

// Create connection
$con = mysql_connect($servername, $username, $password);

//select db
$selected = mysql_select_db("ed",$con);

$json_obj = file_get_contents("stations.json");

//convert to stdclass object
$arr = json_decode($json_obj,true);

//store the element values into variables

$id = $arr[0]["id"];
$name = $arr[0]["name"];
$system_id = $arr[0]["system_id"];
$max_landing_pad_size = $arr[0]["max_landing_pad_size"];
$distance_to_star = $arr[0]["distance_to_star"];
$faction = $arr[0]["faction"];
$government = $arr[0]["government"];
$allegiance = $arr[0]["allegiance"];
$state = $arr[0]["state"];
$type = $arr[0]["type"];
$has_blackmarket = $arr[0]["has_blackmarket"];
$has_commodities = $arr[0]["has_commodities"];
$has_refuel = $arr[0]["has_refuel"];
$has_repair = $arr[0]["has_repair"];
$has_rearm = $arr[0]["has_rearm"];
$has_outfitting = $arr[0]["has_outfitting"];
$has_shipyard = $arr[0]["has_shipyard"];

//insert values into mysql database
$sql="INSERT INTO stations (station_id, name, system_id, max_landing_pad_size, distance_to_star, faction, government, allegiance, state, type, has_blackmarket, has_commodities, has_refuel, has_repair, has_rearm, has_outfitting, has_shipyard) 
VALUES ('$id', '$name', '$system_id', '$max_landing_pad_size', '$distance_to_star', '$faction', '$government', '$allegiance', '$state', '$type', '$has_blackmarket', '$has_commodities', '$has_refuel', '$has_repair', '$has_rearm', '$has_outfitting', '$has_shipyard')";

if(!mysql_query($sql,$con)) //$con is mysql connection object
{
     die('Error : ' . mysql_error());
}
?>

3 个答案:

答案 0 :(得分:0)

使用foreach

$arr = json_decode($json_obj,true);//decode object
foreach($arr as $ar){

            $id = $ar["id"];
            $name = $ar["name"];
            $system_id = $ar["system_id"];
            $max_landing_pad_size = $ar["max_landing_pad_size"];
            $distance_to_star = $ar["distance_to_star"];
            $faction = $ar["faction"];
            $government = $ar["government"];
            $allegiance = $ar["allegiance"];
            $state = $ar["state"];
            $type = $ar["type"];
            $has_blackmarket = $ar["has_blackmarket"];
            $has_commodities = $ar["has_commodities"];
            $has_refuel = $ar["has_refuel"];
            $has_repair = $ar["has_repair"];
            $has_rearm = $ar["has_rearm"];
            $has_outfitting = $ar["has_outfitting"];
            $has_shipyard = $ar["has_shipyard"];

            //insert values into mysql database
            $sql="INSERT INTO stations (station_id, name, system_id, max_landing_pad_size, distance_to_star, faction, government, allegiance, state, type, has_blackmarket, has_commodities, has_refuel, has_repair, has_rearm, has_outfitting, has_shipyard) 
            VALUES ('$id', '$name', '$system_id', '$max_landing_pad_size', '$distance_to_star', '$faction', '$government', '$allegiance', '$state', '$type', '$has_blackmarket', '$has_commodities', '$has_refuel', '$has_repair', '$has_rearm', '$has_outfitting', '$has_shipyard')";

            if(!mysql_query($sql,$con)) //$con is mysql connection object
            {
                 die('Error : ' . mysql_error());
            }

}

答案 1 :(得分:0)

试试这个。

$arr = json_decode($json_obj,true);

$sql = 'INSERT INTO stations (`';

$sql.=  implode('`,`', array_keys( $arr[0] ) );

$sql.= '`) values (\'';

$sql.=  implode('\',\'',  $arr[0]  );

$sql.= '\')';

答案 2 :(得分:0)

对于大量数据,例如在这种情况下,您只想执行一次查询,否则您将不必要地负担数据库负担。为此,您可以使用所有插入的数据构建查询,然后执行它,如下所示:

<?php
$arr = json_decode($json_obj,true);//decode object
$query = "INSERT into stations (station_id, name, system_id, max_landing_pad_size, distance_to_star, faction, government, allegiance, state, type, has_blackmarket, has_commodities, has_refuel, has_repair, has_rearm, has_outfitting, has_shipyard) values ";
foreach($arr as $ar) {    
    $query .= "($ar['id'],$ar['name'],$ar['system_id'],
                $ar['max_landing_pad_size'],$ar['distance_to_star'],$ar['faction'],
                $ar['government'],$ar['allegiance'],$ar['state'],
                $ar['type'],$ar['has_blackmarket'],$ar['has_commodities'],
                $ar['has_refuel'],$ar['has_repair'],$ar['has_rearm'],
                $ar['has_outfitting'],$ar['has_shipyard']),";
}
$query = rtrim(",",$query);
if(!mysql_query($query,$con)) //$con is mysql connection object
{
    die('Error : ' . mysql_error());
}

如果您想知道,您的原始代码无法正常工作,因为您只是从json($ arr [0])抓取第一行。您需要遍历数据以获取所有行。