I have been tasked with trying to map a json file to a database a sample of the json is given below:
configurationItems":[
{
"configurationItemVersion":"1.0",
"configurationItemCaptureTime":"2014-12-05T10:22:51.751Z",
"configurationStateId":1,
"relatedEvents":[ ],
"awsAccountId":"val",
"configurationItemStatus":"v",
"resourceId":"fg",
"ARN":"ggggg",
"awsRegion":"us-east-1",
"availabilityZone":"us-east-1b",
"configurationStateMd5Hash":"ggd45",
"resourceType":"AWS::EC2::Instance",
"resourceCreationTime":"2014-01-06T10:37:37.000Z",
"tags":{
"Name":"",
"cirrushq_id":""
},
using the following code I have been able to map the entries down to resource creation time but now wish to map the tags to a different table called tag
<?php
$con=mysqli_connect("localhost","root","","json_map");
$response = array();
$res=array();
$json = file_get_contents('C:\Users\Richard\Desktop\test.json');
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 />";
etc etc
$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);
}
}
}
?>
I tried duplicating the code into a separate file and altering the insert statement to
if(is_array($decoded["configurationItems"]))
{
foreach($decoded["configurationItems"] as $configurationItems)
//for($i=0;$i>sizeof($decoded["configurationItems"]);$i++)
{
$Name=$configurationItems["Name"];
echo "<br />","Name:",$Name,"<br />";
$cirrushq_id=$configurationItems["cirrushq_id"];
echo "<br />","cirrushq_id:",$cirrushq_id,"<br />";
$result = mysqli_query($con, "INSERT INTO tag(name, cirrushq_id)
VALUES('$Name','$cirrushq_id' )")or die("Insert Failed ".((is_object($con)) ? mysqli_error($con) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));;
but to no avail it just returns unknown index Name
From what you wrote, I'm not completely sure to understand what's the cause of your problem.
However, let me point out a few inconsistencies I spotted:
1) Since you speak about tags (plural), I'm guessing you'd like to have more than one tag associated to each configurationItems element. In this case your JSON should be:
...
"tags":[{
"Name":"",
"cirrushq_id":""
},{
"Name":"",
"cirrushq_id":""
}]
...
2) According your JSON, to access tag name and tag cirrushq_id you should do:
$Name=$configurationItems["tags"]["Name"];
$cirrushq_id=$configurationItems["tags"]["cirrushq_id"];
and in the case you take into consideration my point 1), you'd have to make another loop through the tags:
foreach($decoded["configurationItems"] as $configurationItems)
{
...
foreach($decoded["configurationItems"]["tags"] as $tags)
{
$Name=$tags["Name"];
echo "<br />","Name:",$Name,"<br />";
$cirrushq_id=$tags["cirrushq_id"];
echo "<br />","cirrushq_id:",$cirrushq_id,"<br />";
// clearly, the INSERT query should also be inside this nested loop
}
}
3) From what you wrote in the tag INSERT query, you have a lowercase name column, while in your JSON, the index Name is uppercase...I know it's trivial but sometimes, when there's not another pair of eyes that can double-check your code, it's just simple stuff like this that make you waste a lot of time.
Hope to have been of help!
Related
EDIT: SOLVED. Thanks to the guys in the comments who recommended turning PHP error messages on, showed me exactly where things were going wrong. For those that are curious, it was as simple as this: two function calls had "mysql_" instead of "mysqli_". Also thanks to the user that pointed out that prepared SQL queries must use ->execute() instead of mysqli_query().
I'm working on a community bulletin board. I've gotten it to a point where the page for posting a listing works and submits it to the database, but when I try to search for the listings I'm having some trouble with the SQL query. Here's the code that I have:
<?php
if(isset($_GET['submit'])){
$location = $_GET['location'];
echo "Looking for listings in: " . $location;
echo "<br />";
require_once('mysqli_connect.php');
$query = "
SELECT email_address
, phone_number
, date
, listing_content
FROM listings
WHERE location='?'
";
echo $query;
$stmt = mysqli_prepare($dbc, $query);
mysqli_stmt_bind_param($stmt, "s", $location);
$results = mysqli_query($stmt);
$numListings = mysql_num_rows($results);
echo $numListings . " listing(s) found";
echo "<hr>";
if ($numListings == 0){
echo "Feel free to press the button above to post the first listing in " .
$location;
} else {
while($row = mysql_fetch_array($results)){
echo "Location: " . $location;
echo "<br />";
echo "Date: " . $row[date];
echo "<br />";
echo "Email address: " . $row[email_address];
echo "<br />";
if($row[phone_number] != NULL){
echo "Phone number: " . $row[phone_number];
echo "<br />";
}
echo $row[listing_content];
echo "<br />";
echo "<hr>";
}
}
mysqli_stmt_close($stmt);
mysqli_close($dbc);
}
?>
And this is the output I'm getting, where everything below the button "Make new post" is generated by the PHP:
It looks like it's crashing, at some point after the line echo $query; because it prints that out but it doesn't print some of the other statements after that. Either it's crashing or the SQL query is somehow messing up without crashing it or throwing an error.
This is what the database looks like:
I have thousand of data from an webservice .
but nusoap limit execution time only 30 second .
I can't insert all those data because of this limit .
I can't change nusoap setting because is on server .
.
is there anyway to fix that ?
.
EDIT 1
i tried split big data to array chunk, but after 30 second is over
here's my code (using code igniter)
$this->db->trans_start();
$_datas = array_chunk($data["result"], 300);
foreach ($_datas as $key => $data) {
$insert=$this->db->insert_batch('temp_mahasiswa', $data);
if (!$insert && $this->db->error()) {
//some logics here, you may create some string here to alert user
echo "Data nim"; echo " "; echo $key['nipd'] ; echo " "; echo "Sudah Ada"; echo '<br>';
}else {
//other logics here
echo "Data nim"; echo " "; echo $key['nipd'] ; echo " "; echo "Sudah Masuk"; echo '<br>';
}
}
$this->db->update('temp_mahasiswa',$tambah);
$this->db->trans_complete();
Way 1
You can insert data with LOAD DATA: http://dev.mysql.com/doc/refman/5.7/en/load-data.html
it is much faster than INSERT.
Way2
You can create queue (for example with Beanstalk) and background job what will insert data.
Hi I am working on trying to get my app to load json files from a folder and decode to a database using the following code :
<?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);
}
}
}
?>
I know it will be something stupid like a bracket but even using an IDE I am still no seeing it
Other than the missing foreach on line 7 (I'm assuming a mistake, because it would give a different syntax error message).
Your actual syntax error is a missing closing brace. Add } to the end of your code before the closing ?> and you have valid syntax. Whether you have working code is another matter. Try formatting your code with better indentation and you will more easily see where the missing tokens should be.
As a side note: A closing ?> is not required in PHP and I recommend against it.
I have this search php code in which i made the formnumber into a hyperlink. All I wanna do is once i click a formnumber it will retrieve all ta data of tha click formnumber into the database and echos it.
$dbname = "vianney300";
$SRCHDATA = $_POST["srch"];
if($connection===FALSE)
echo "<p> Connection Failed. ". mysql_error()."</p>";
else{
if(mysql_select_db( $dbname)===FALSE)
echo "<p>Could not select database.</p>";
}
$query = "SELECT * FROM profile WHERE LastName='$SRCHDATA'";
$result = #mysql_query($query);
if ($result===FALSE){
echo "<p>Unable to execute query.</p>";
}
else {
while (($row = mysql_fetch_assoc($result))!==FALSE){
echo "Form no: ";
echo "<a href='individual.php'>{$row['FormNo']}</a></br>";
echo "Last name: ";
echo "{$row['LastName']}</br>";
echo "First name: ";
echo "{$row['FirstName']}</br></br>";
If you want to send a form number to individual page then you have to do like this inside your while function :
echo "<a href='individual.php?form_no={$row['FormNo']}'>{$row['FormNo']}</a></br>";
And on individual.php you will retrieve form number using $_GET['form_no']
individual.php
<?php
echo $_GET['form_no'];
// Using this form number you can fetch the data from database
?>
You have made a small mistake on this line
echo "<a href='individual.php'>{$row['FormNo']}</a></br>";
it should be
echo "<a href='individual.php?formnumber={$row['FormNo']}'>{$row['FormNo']}</a></br>";
ok.then what should i put to echo in the individual.php
Thats another question really, but
code individual.php
<?php
if ( isset($_GET['formnumber']) ) {
echo 'Recieved parameter "formnumber" in the GET array = ' . $_GET['formnumber'];
} else {
echo 'No parameter passed';
}
?>
<?php
$con=mysqli_connect("localhost","root","","clarks");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$place = $_GET['place'];
$place2 = $_GET['place2'];
$return = $_GET['return'];
$people = $_GET['people'];
$pickup = $_GET['pickup'];
$dropoff = $_GET['dropoff'];
$result = mysqli_query($con,"SELECT * FROM pricelist WHERE place1='$place' AND place2='$place2' AND people='$people'");
while($row = mysqli_fetch_array($result))
{
if (!empty($row['Price']))
{
echo "Not Applicable";
}
else
{
echo "<html><body style='background-color: #31ff01;'><link rel='stylesheet' href='css/bootstrap.min.css'>";
echo "<div id='prices'>£";
echo $row['Price'] * $return + $pickup + $dropoff;
echo "</div>";
echo "<div id='back'>";
echo "<a href='index.html'>Go Back</a>";
echo "</div>";
echo "<br>";
}
}
mysqli_close($con);
?>
This is returning the "Not Applicable" from the if argument every time.
I've tried placing the lines in different order and using both the price and result variable, it still only returns not applicable but without the if else argument, the rest of it works as intended and brings up a price so I know the price variable shouldn't be empty and neither should the result variable.
With it returning not applicable all the time, I assume it's not retrieving the information from the database properly any more but I can't figure out why when it works perfectly fine without the if else.
Any help would be great, Thanks.
Your logic is wrong:
if (!empty($row['Price']))
^ here
{
echo "Not Applicable";
}
should be:
if (empty($row['Price']))
{
echo "Not Applicable";
}
You also have a serious sql injection problem, you should use prepared statements or at the very least use mysqli's escaping function.
if (empty($row['Price']))
{
echo "Not Applicable";
}
else
{
echo "<html><body style='background-color: #31ff01;'><link rel='stylesheet'
.....
}
}
I think the in the if condition the negation causes the problem
Remove the exclamation mark!
It might be because you are using mysqli_fetch_array instead of mysqli_fetch_assoc. http://www.php.net/manual/en/mysqli-result.fetch-assoc.php
You should also start using real_ escape_ string on your parameters, or do it in the right way with parameterized prepared statement. http://www.php.net/manual/en/mysqli.prepare.php
<?php
$con=mysqli_connect("localhost","root","","clarks");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$place = $_GET['place'];
$place2 = $_GET['place2'];
$return = $_GET['return'];
$people = $_GET['people'];
$pickup = $_GET['pickup'];
$dropoff = $_GET['dropoff'];
$result = mysqli_query($con,"SELECT * FROM pricelist WHERE place1='$place' AND place2='$place2' AND people='$people'");
$row = (mysqli_fetch_array($result));
$a = $row[3];
if (empty($a))
{
echo "<html><body style='background-color: #31ff01;'><link rel='stylesheet' href='css/bootstrap.min.css'>";
echo "<div id='prices'>";
echo "N/A";
echo "</div>";
echo "<div id='back'>";
echo "<a href='index.html'>Go Back</a>";
echo "</div>";
echo "<br>";
}
else
{
echo "<html><body style='background-color: #31ff01;'><link rel='stylesheet' href='css/bootstrap.min.css'>";
echo "<div id='prices'>£";
echo $a * $return + $pickup + $dropoff;
echo "</div>";
echo "<div id='back'>";
echo "<a href='index.html'>Go Back</a>";
echo "</div>";
echo "<br>";
}
mysqli_close($con);
?>
Well I found this workaround, instead of using while I just got rid of it and assigned the price from the database into its own variable meaning there's no conflict with the if else statement. Works like a charm... now to look at prepared statements :S