简体   繁体   中英

Sending JSON information to database with PHP?

I'm trying to send the data from a JSON file into a MySQL database using PHP.

I have it working 99% but have run into a small snag I can't figure out. Here's my code:

$con = mysqli_connect($host, $username, $password, $dbname) or die('Error in Connecting: ' . mysqli_error($con));

$st = mysqli_prepare($con, 'INSERT INTO url_feed(url, results, current_date, networks, identifier) VALUES (?, ?, ?, ?, ?)');

mysqli_stmt_bind_param($st, 'sssss', $url, $results, $current_date, $networks, $identifier);

$filename = 'https://www.example.com/random.json';
$json = file_get_contents($filename);   

$data = json_decode($json, true);

foreach ($data as $row) {
$url = $row['url'];
$identifier = $row['identifier'];
$current_date = $row['current_date'];
$results = $row['results'];
$networks = $row['networks'];

    mysqli_stmt_execute($st);
}

mysqli_close($con);

Here's a copy of a the JSON with 3 objects in it:

[
   {
      "url":"http://example1.com",
      "identifier":495755330,
      "current_date":"2015-12-30 17:05:45",
      "results":3,
      "networks":{
         "FaceBook":{"detected":true,"result":"no-result"},
         "Twitter Inc":{"detected":false,"result":"no-result"},
         "Pinterest.com":{"detected":true,"result":"no-result"},
         "Other Sites":{"detected":true,"result":"some-result"}
      }
   },
   {
      "url":"http://example2.com",
      "identifier":495755331,
      "current_date":"2015-12-30 17:05:46",
      "results":0,
      "networks":{
         "FaceBook":{"detected":false,"result":"what-result"},
         "Twitter Inc":{"detected":false,"result":"some-result"},
         "Pinterest.com":{"detected":false,"result":"some-result"},
         "Other Sites":{"detected":false,"result":"what-result"}
      }
   },
   {
      "url":"http://example3.com",
      "identifier":495755332,
      "current_date":"2015-12-30 17:05:47",
      "results":1,
      "networks":{
         "FaceBook":{"detected":false,"result":"some-result"},
         "Twitter Inc":{"detected":true,"result":"some-result"},
         "Pinterest.com":{"detected":false,"result":"some-result"},
         "Other Sites":{"detected":false,"result":"some-result"}
      }
   }
]

Right now if I run the script, it inserts it into the database like this:

id | url                 | results   | current_date        | networks | identifier  | status
1  | http://example1.com | 3         | 2015-12-30 17:05:45 | Array    | 495755330   | queued
2  | http://example2.com | 0         | 2015-12-30 17:05:46 | Array    | 495755331   | queued
3  | http://example3.com | 1         | 2015-12-30 17:05:47 | Array    | 495755332   | queued

But this is how I want it to be entered:

id | url                 | results   | current_date        | networks                           | identifier | status
1  | http://example1.com | 3         | 2015-12-30 17:05:45 | FaceBook,Pinterest.com,Other Sites | 495755330  | queued
2  | http://example3.com | 1         | 2015-12-30 17:05:47 | Twitter Inc                        | 495755332  | queued

Here's the part I can't figure out:

It's trying to enter as an Array, which obviously isn't working - and it just insert the text "Array" under the networks column. I only want to insert into the database if detected is set to true . If it is not, I don't want that social network listed in the database.

If none of the social networks are set to true for an object, I don't want that object entered at all. This is why you see certain networks missing from my above example and why the second row isn't there.

Before inserting the values into database do some checks on values of $netwrok. only when values are validated then insert

    foreach ($data as $row) {
$url = $row['url'];
$identifier = $row['identifier'];
$current_date = $row['current_date'];
$results = $row['results'];
$networks = $row['networks'];
//**insert you checks here ***//
    mysqli_stmt_execute($st);
}
$data = json_decode($json, true);

foreach ($data as $row) {
    $url = $row['url'];
    $identifier = $row['identifier'];
    $current_date = $row['current_date'];
    $results = $row['results'];
    $network_row = $row['networks'];

    $networks = '';

    foreach($network_row as $key => $val) {
        if ($val->detected == true) {
            $networks .= $key . ',';
        }
    }

    if (mb_strlen($networks, 'utf-8') > 0) {
        $networks = substr($networks, 0, mb_strlen($networks, 'utf-8')-1);
        mysqli_stmt_execute($st);
    }
}

Use the implode() function as part of the insert. You can then use the explode() function when retrieving.

http://php.net/manual/en/function.implode.php

Instead of $networks = $row['networks']; Use this:

$a = json_decode($row['networks'], true);
$b = array_filter($a, function($el) {
  if ($el['detected'] == true) {
    return true;
  }
});
$c = implode(', ', array_keys($b));
$networks = $c;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM