1

this is my json data, i am try to decode this json and store the data in mysql database. i am try json decoding in php. In my php server side json data are not decoded and not stored in mysql database. please help me to solve this problem.

JSON DATA

{  
   "code":"0",
   "message":"Success",
   "events":[  
      {  
         "id":1,
         "name":"test event",
         "description":"test desc",
         "date":"2014-06-22 00:00:00",
         "location":"keelapalur",
         "type":1,
         "passcode":"123456",
         "created":{  
            "date":"2014-06-08 17:05:12",
            "timezone_type":3,
            "timezone":"UTC"
         },
         "updated":{  
            "date":"2014-06-08 17:05:12",
            "timezone_type":3,
            "timezone":"UTC"
         }
      },
      {  
         "id":2,
         "name":"rtyr",
         "description":"rtyr",
         "date":"2014-06-22 00:00:00",
         "location":"try",
         "type":1,
         "passcode":"123456",
         "created":{  
            "date":"2014-06-12 09:26:31",
            "timezone_type":3,
            "timezone":"UTC"
         },
         "updated":{  
            "date":"2014-06-12 09:26:31",
            "timezone_type":3,
            "timezone":"UTC"
         }
      }
   ]
}

PHP code

<?php 
$response = array();
$res=array();
    require_once __DIR__ . '/db_connect.php';
    $json = file_get_contents('/insert.json');
    if($json!=null){
    $decoded=json_decode($json,true);
    //$decode= var_dump($decoded);
    //$ss=$decode["array"];
    //echo $decoded['number'];
    if(sizeof($decoded["events"])>0)
    {
    for($i=0;$i>sizeof($decoded["events"]);$i++)
    {
    $id=$decoded["events"]["id"];
$name=$decoded["events"]["name"];
$date=$decoded["events"]["date"];
$desc=$decoded["events"]["description"];
$location=$decoded["events"]["location"];
$type=$decoded["events"]["type"];
$passcode=$decoded["events"]["passcode"];
$cdate=$decoded["events"]["created"]["date"];
$ctimzonety=    $decoded["events"]["created"]["timezone_type"];
$ctimz=$decoded["events"]["created"]["timezone"];
$udate=$decoded["events"]["updated"]["date"];
$utimzonety=    $decoded["events"]["updated"]["timezone_type"];
$utimz=$decoded["events"]["updated"]["timezone"];

$result = mysql_query("INSERT INTO events(userid,name,desc,date,loc,type,passcode,cdate,ctimezone_type,ctimezone,udate,utimezone_type,utimezone) 
VALUES('$id,'$name','$desc','$date','$loc','$type','$passcode','$cdate','$ctimzonety','$ctimz','$udate','$utimzonety','$utimz')");

    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["code"] = 1;
        $response["message"] = "successfully stored";

        // 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);
    }



}
    }
    }
?>
3
  • 1
    Transform it to an object and then iterate over it to insert in the database the data in the correct place. I don't know PHP but it shouldn't be very difficult. Commented Aug 26, 2014 at 14:02
  • so what's the problem? what's (not) happening? Commented Aug 26, 2014 at 14:05
  • my json decoding code is wrong ,its main problem for me Commented Aug 26, 2014 at 14:07

1 Answer 1

4

Make ensure your database connectivity..Try this code 100% it's working...

<?php 
$response = array();
$res=array();

    $json = file_get_contents('C:\Users\Devunne3\Desktop\insert.json');

    if($json!=null){
    $decoded=json_decode($json,true);
    //$decode= var_dump($decoded);
    //$ss=$decode["array"];
    //echo $decoded['number'];
    if(is_array($decoded["events"]))
    {
    foreach($decoded["events"] as $events)
    //for($i=0;$i>sizeof($decoded["events"]);$i++)

    {
    $id=$events["id"];
    echo "<br />","userid:",$id,"<br />";

$name=$events["name"];
echo "name:",$name,"<br />";
$date=$events["date"];
echo "date:",$date,"<br />";
$desc=$events["description"];
echo "desc:",$desc,"<br />";
$location=$events["location"];
echo "loc:",$location,"<br />";
$type=$events["type"];
echo "type:",$type,"<br />";
$passcode=$events["passcode"];
echo "passcode:",$passcode,"<br />";
$cdate=$events["created"]["date"];
echo "cdate:",$cdate,"<br />";
$ctimzonety=$events["created"]["timezone_type"];
echo "ctimezone_type:",$ctimzonety,"<br />";
$ctimz=$events["created"]["timezone"];
echo "ctimezone:",$ctimz,"<br />";
$udate=$events["updated"]["date"];
echo "udate:",$udate,"<br />";
$utimzonety=$events["updated"]["timezone_type"];
echo "utimezone_type:",$utimzonety,"<br />";
$utimz=$events["updated"]["timezone"];
echo "utimezone",$utimz,"<br />";
echo"------------------------------------------------","<br />";

require_once __DIR__ . '/db_connect.php';
 $db = new DB_CONNECT();

$result = mysql_query("INSERT INTO events(userid,name,desc,date,loc,type,passcode,cdate,ctimezone_type,ctimezone,udate,utimezone_type,utimezone)
VALUES('$id','$name','$desc','$date','$location','$type','$passcode','$cdate','$ctimzonety','$ctimz','$udate','$utimzonety','$utimz')")or die("Insert Failed ".mysql_error());;

    }// check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["code"] = 1;
        $response["message"] = "successfully stored";

        // 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);
    }

    }
    }



?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.