Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript PHP解码JSON数组并插入MYSQL数据库_Javascript_Php_Mysql_Arrays_Json - Fatal编程技术网

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

Javascript PHP解码JSON数组并插入MYSQL数据库,javascript,php,mysql,arrays,json,Javascript,Php,Mysql,Arrays,Json,我正在尝试创建一个PHP脚本,用于解码JSON数组并将其插入数据库。到目前为止,我已经设法让脚本在数组中插入第一行,而没有其他内容 为了让脚本插入数组中的所有行,我需要添加什么 这是阵列,忽略“列表”,我还不需要这些数据(它相当大): 以下是脚本: 使用foreach $arr = json_decode($json_obj,true);//decode object foreach($arr as $ar){ $id = $ar["id"];

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

为了让脚本插入数组中的所有行,我需要添加什么

这是阵列,忽略“列表”,我还不需要这些数据(它相当大): 以下是脚本:

使用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());
            }

}
试试这个

$arr = json_decode($json_obj,true);

$sql = 'INSERT INTO stations (`';

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

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

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

$sql.= '\')';

对于像本例中这样的大量数据,您可能希望只执行一次查询,否则会给数据库带来不必要的负担。为此,您可以使用插入的所有数据构建查询,然后执行查询,如下所示:

<?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());
}

<代码>如果它真的很大,你可以考虑另一种方法,首先创建一个查询字符串,它可以让你创建一个插入批处理,然后运行它。或者创建一个文件,然后加载数据本地填充。这比一行一行地循环插入要快得多php似乎抛出了一个错误:“注意:未定义的变量:C:\wamp\www\index.php中的arr在第17行”当它这样做时,我得到了这个结果:“注意:C:\wamp\www\index.php中的数组到字符串转换在第42行”您需要确保$arr[0]拥有所有的“键”=>“值”组合。