user10848
user10848

Reputation: 161

Entering JSON data into MySQL

I'm trying to get JSON data and enter it into my database.

Here's my current code:

while($row= mysqli_fetch_assoc($query)){
    $id = $row["id"];
    $domain = $row["domain"];
    $time = date('Y-m-d H:i:s');

    $ch = curl_init();
    $url = "https://www.example.com/";
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "apikey=123&test=$domain");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec ($ch);
    print $output;

    // CODE SHOULD BE ADDED HERE I THINK

    curl_close ($ch);

    $sql_to_update = "UPDATE monitoring SET socials='$socials', update_time='$time' WHERE id='$id'";
    mysqli_query($conn,$sql_to_update);
}

Here's an example of the JSON results I'm getting:

{
   "resource":"random data",
   "tests":100,
   "total":250,
   "networks":{
      "FaceBook":{
         "detected":true,
         "result":"ignore"
      },
      "Twitter Inc":{
         "detected":false,
         "result":"ignore"
      },
      "MySpace":{
         "detected":true,
         "result":"ignore"
      },
      "Pinterest":{
         "detected":true,
         "result":"ignore"
      },
      "Instagram":{
         "detected":false,
         "result":"ignore"
      }
   }
}

All that I need to do is get the networks that have detected set to true and insert them into my database using the variable $socials.

For example, for the JSON example listed above, I'd want the following entered into the socials column of the database: FaceBook, MySpace, Pinterest

If there are no networks that have detected set to true, then I want to enter NONE FOUND into that column instead.

Anyone know how I can work with the JSON output to get this into my database?

Upvotes: 2

Views: 129

Answers (2)

Aniruddha Chakraborty
Aniruddha Chakraborty

Reputation: 1867

Make it an array for better handling json_decode($json, true)

echo '<pre>';
print_r(json_decode($json, true));
echo '</pre>';

You'll get a stdClass object like this

stdClass Object
(
    [resource] => random data
    [tests] => 100
    [total] => 250
    [networks] => stdClass Object
        (
            [FaceBook] => stdClass Object
                (
                    [detected] => 1
                    [result] => ignore
                )

            [Twitter Inc] => stdClass Object
                (
                    [detected] => 
                    [result] => ignore
                )

            [MySpace] => stdClass Object
                (
                    [detected] => 1
                    [result] => ignore
                )

            [Pinterest] => stdClass Object
                (
                    [detected] => 1
                    [result] => ignore
                )

            [Instagram] => stdClass Object
                (
                    [detected] => 
                    [result] => ignore
                )

        )

)

then perform a loop on the array . I hope you know the rest . what should you do now.

Upvotes: 1

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

Check the code to get the desired data that you want to update:-

<?php
$link = '{
   "resource":"random data",
   "tests":100,
   "total":250,
   "networks":{
      "FaceBook":{
         "detected":true,
         "result":"ignore"
      },
      "Twitter Inc":{
         "detected":false,
         "result":"ignore"
      },
      "MySpace":{
         "detected":true,
         "result":"ignore"
      },
      "Pinterest":{
         "detected":true,
         "result":"ignore"
      },
      "Instagram":{
         "detected":false,
         "result":"ignore"
      }
   }
}'; // suppose json variable is $link
$dataArr = json_decode($link);
echo "<pre/>";print_r($dataArr);// print the variable to see how json data looks when it converted to php array
$socials = '';
foreach($dataArr->networks as $key=>$dataA){
    if($dataA->detected == 1){
        $socials .= $key.',';
    }

}
$socials = trim($socials,',');
echo $socials;
?>

Output:-https://eval.in/506693

Now put your update code and that's it.

Upvotes: 1

Related Questions