您好我正在尝试让我的应用程序从文件夹加载json文件并使用以下代码解码到数据库:
<?php
$con = mysqli_connect("localhost", "root", "", "json_map");
$response = array();
$res = array();
foreach(glob('C:\xampp\htdocs\laravel\awsconfig\app\JSON_Files') 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++)
{
$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 />";
$ARN = $configurationItems["ARN"];
echo "ARN:", $ARN, "<br />";
$awsRegion = $configurationItems["awsRegion"];
echo "awsRegion:", $awsRegion, "<br />";
$availabilityZone = $configurationItems["availabilityZone"];
echo "availabilityZone:", $availabilityZone, "<br />";
$configurationStateMd5Hash = $configurationItems["configurationStateMd5Hash"];
echo "configurationStateMd5Hash:", $configurationStateMd5Hash, "<br />";
$resourceType = $configurationItems["resourceType"];
echo "resourceType:", $resourceType, "<br />";
$resourceCreationTime = $configurationItems["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 stored configuration items ";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["code"] = 2;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
}
}
?>
我知道它会像支架一样愚蠢,但即使使用IDE我仍然没有看到它
答案 0 :(得分:1)
除了第7行丢失的foreach
之外(我假设错误,因为它会给出不同的语法错误消息)。
您的实际语法错误是缺少右括号。在结束}
之前将?>
添加到代码的末尾,并且您具有有效的语法。你是否有工作代码是另一回事。尝试使用更好的缩进格式化代码,您将更容易看到缺少的标记应该在哪里。
作为旁注:PHP中不需要结束?>
,我建议不要这样做。