簡體   English   中英

使用PHP將JSON信息發送到數據庫?

[英]Sending JSON information to database with PHP?

我正在嘗試使用PHP將數據從JSON文件發送到MySQL數據庫。

我有99%的工作,但遇到了一個我無法弄清楚的小問題。 這是我的代碼:

$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);

這是一個包含3個對象的JSON的副本:

[
   {
      "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"}
      }
   }
]

現在如果我運行腳本,它會將它插入到數據庫中,如下所示:

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

但這就是我想要輸入的方式:

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

這是我無法弄清楚的部分:

它試圖作為一個數組輸入,這顯然不起作用 - 它只是在networks列下插入文本“數組”。 如果detected設置為true我只想插入數據庫。 如果不是,我不希望數據庫中列出的社交網絡。

如果某個對象的社交網絡都沒有設置為true ,我根本不希望輸入該對象。 這就是為什么你看到我上面的例子中缺少某些網絡以及第二行不存在的原因。

在將值插入數據庫之前,請對$ netwrok的值進行一些檢查。 只有在驗證了值時才插入

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

使用implode()函數作為插入的一部分。 然后,您可以在檢索時使用explode()函數。

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

而不是$networks = $row['networks']; 用這個:

$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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM