我正在尝试根据JSON中特定元素的值将JSON文件映射到我想要的mysql数据库中,如果它等于该值,则将条目放入数据库中但是当我运行我的应用程序时它返回所有内容< / p>
php for table 1,它应该检查资源类型是否等于实例将条目放入表中:
<?php
$con=mysqli_connect("localhost","root","","json_map");
$response = array();
$res=array();
$result='' ;
foreach(glob('C:\xampp\htdocs\laravel\awsconfig\app\views\*.json') as $filename)
{
$json = file_get_contents($filename);
if($json!=null)
{
$decoded=json_decode($json,true);
//$decode= var_dump($decoded);
//$ss=$decode["array"];
//echo $decoded['number'];
if(is_array($decoded["configurationItems"]))
{
foreach($decoded["configurationItems"] as $configurationItems)
{
//for($i=0;$i>sizeof($decoded["configurationItems"]);$i++)
$cfi=$configurationItems;
if(isset($cfi["resourceType"]["AWS::EC2::Instance"]));
if(isset($cfi["configurationItemVersion"]) && isset($cfi["configurationItemCaptureTime"]) && isset($cfi["configurationStateId"])&& isset($cfi["awsAccountId"]) && isset($cfi["awsAccountId"]) )
{
$configurationItemVersion=$configurationItems["configurationItemVersion"];
echo "<br />","configuration_Item_Version:",$configurationItemVersion,"<br />";
$configurationItemCaptureTime=$configurationItems["configurationItemCaptureTime"];
echo "configurationItemCaptureTime:",$configurationItemCaptureTime,"<br />";
$configurationStateId=$configurationItems["configurationStateId"];
echo "configurationStateId:",$configurationStateId,"<br />";
$awsAccountId=$configurationItems["awsAccountId"];
echo "awsAccountId:",$awsAccountId,"<br />";
$configurationItemStatus=$configurationItems["configurationItemStatus"];
echo "configurationItemStatus:",$configurationItemStatus,"<br />";
$resourceId=$configurationItems["resourceId"];
echo "resourceId:",$resourceId,"<br />";
$configurationStateMd5Hash=$configurationItems["configurationStateMd5Hash"];
echo "configurationStateMd5Hash:",$configurationStateMd5Hash,"<br />";
$resourceType=$configurationItems["resourceType"];
echo "resourceType:",$resourceType,"<br />";
$ARN= isset($cfi["ARN"]) ? $cfi["ARN"] : '';
echo "ARN:",$ARN,"<br />";
$awsRegion= isset($cfi["awsRegion"]) ? $cfi["awsRegion"] : '';
echo "awsRegion:",$awsRegion,"<br />";
$availabilityZone= isset($cfi["availabilityZone"]) ? $cfi["availabilityZone"] : '';
echo "availabilityZone:",$availabilityZone,"<br />";
$resourceCreationTime= isset($cfi["resourceCreationTime"]) ? $cfi["resourceCreationTime"] : '';
echo "resourceCreationTime:",$resourceCreationTime,"<br />";
$result = mysqli_query($con, "INSERT INTO configuration_item(configuration_item_version,configuration_item_capture_time,configuration_state_id, aws_account_id, configuration_item_status, resource_id, arn, aws_region, availability_zone,configuration_state_md5_hash, resource_type, resource_creation_time)
VALUES('$configurationItemVersion','$configurationItemCaptureTime','$configurationStateId','$awsAccountId','$configurationItemStatus','$resourceId','$ARN','$awsRegion','$availabilityZone','$configurationStateMd5Hash','$resourceType','$resourceCreationTime' )")or die("Insert Failed ".((is_object($con)) ? mysqli_error($con) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));;
}
// check if row inserted or not
if ($result)
{
// successfully inserted into database
$response["code"] = 1;
$response["message"] = "successfully updated configuration items ";
// echoing JSON response
echo json_encode($response);
}
else
{
echo mysql_error();
// failed to insert row
$response["code"] = 2;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
}
}
}
}
&GT?;
php for table 2,它应该如上所述,但是在相同的音量中放入另一个表:
<?php
$con=mysqli_connect("localhost","root","","json_map");
$response = array();
$res=array();
$result='' ;
foreach(glob('C:\xampp\htdocs\laravel\awsconfig\app\views\*.json') as $filename)
{
$json = file_get_contents($filename);
if($json!=null)
{
$decoded=json_decode($json,true);
//$decode= var_dump($decoded);
//$ss=$decode["array"];
//echo $decoded['number'];
if(is_array($decoded["configurationItems"]))
{
foreach($decoded["configurationItems"] as $configurationItems)
{
//for($i=0;$i>sizeof($decoded["configurationItems"]);$i++)
$cfi=$configurationItems;
if(isset($cfi["resourceType"]["AWS::EC2::Volume"]));
{
$configurationItemVersion=$cfi["configurationItemVersion"];
echo "<br />","configuration_Item_Version:",$configurationItemVersion,"<br />";
$configurationItemCaptureTime=$cfi["configurationItemCaptureTime"];
echo "configurationItemCaptureTime:",$configurationItemCaptureTime,"<br />";
$configurationStateId=$cfi["configurationStateId"];
echo "configurationStateId:",$configurationStateId,"<br />";
$awsAccountId=$cfi["awsAccountId"];
echo "awsAccountId:",$awsAccountId,"<br />";
$configurationItemStatus=$cfi["configurationItemStatus"];
echo "configurationItemStatus:",$configurationItemStatus,"<br />";
$resourceId=$cfi["resourceId"];
echo "resourceId:",$resourceId,"<br />";
$configurationStateMd5Hash=$cfi["configurationStateMd5Hash"];
echo "configurationStateMd5Hash:",$configurationStateMd5Hash,"<br />";
$resourceType=$cfi["resourceType"];
echo "resourceType:",$resourceType,"<br />";
$ARN= isset($cfi["ARN"]) ? $cfi["ARN"] : '';
echo "ARN:",$ARN,"<br />";
$awsRegion= isset($cfi["awsRegion"]) ? $cfi["awsRegion"] : '';
echo "awsRegion:",$awsRegion,"<br />";
$availabilityZone= isset($cfi["availabilityZone"]) ? $cfi["availabilityZone"] : '';
echo "availabilityZone:",$availabilityZone,"<br />";
$resourceCreationTime= isset($cfi["resourceCreationTime"]) ? $cfi["resourceCreationTime"] : '';
echo "resourceCreationTime:",$resourceCreationTime,"<br />";
{
}
$result = mysqli_query($con, "INSERT INTO cfi_vol(configuration_item_version,configuration_item_capture_time,configuration_state_id, aws_account_id, configuration_item_status, resource_id, arn, aws_region, availability_zone,configuration_state_md5_hash, resource_type, resource_creation_time)
VALUES('$configurationItemVersion','$configurationItemCaptureTime','$configurationStateId','$awsAccountId','$configurationItemStatus','$resourceId','$ARN','$awsRegion','$availabilityZone','$configurationStateMd5Hash','$resourceType','$resourceCreationTime' )")or die("Insert Failed ".((is_object($con)) ? mysqli_error($con) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));;
// check if row inserted or not
if ($result)
{
// successfully inserted into database
$response["code"] = 1;
$response["message"] = "successfully updated configuration items volume";
// echoing JSON response
echo json_encode($response);
}
else
{
echo mysql_error();
// failed to insert row
$response["code"] = 2;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
}
}
}
}
}
?>
不幸的是,当我运行这个时,我得到了一切返回
答案 0 :(得分:1)
请在if语句后删除分号
if(isset($cfi["resourceType"]["AWS::EC2::Volume"]))
同时检查以下行。
if(isset($cfi["resourceType"]["AWS::EC2::Instance"]));