簡體   English   中英

將json數據插入mysql表

[英]Insert json data into mysql table

我知道解析json數據已有很多討論,但我想要的可能更簡單

我需要對bleow php腳本進行一些編輯,以將其轉換為json數據並將其推入MySQL表,因為該腳本僅可讀取json數據! 我對php編碼不太熟悉。

任何幫助都需要提前感謝。

<?php

$data_string = '{"para": {"psize":"1","date_offset":"now","lang":"en","page":1,"token":"class","subcat  ":"15"},"req":"ne"}';
$ch = curl_init('http://exampe.com/websrv/');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);

$result = curl_exec($ch);
header('Content-Type: text/plain; charset=utf-8');
print_r(json_decode($result));

?>

而我得到的結果

stdClass Object
(
[data] => Array
    (
        [0] => stdClass Object
            (
                [is_fav] => 0
                [is_new] => 1
                [description] =>   Panasonic  
                [is_sold] => 0
                [language] => en
                [image] => 
                [contact_no] => 55561112
                [is_pinned] => 0
                [user_adv_id] => 1234
                [premium_tag] => 0
                [keywords] => 
                [title] => for sale Panasonic
                [is_not_abusive] => 0
                [announce_date] => 2015-01-01 02:33:33
                [user_id] => 13
                [price] => 20
                [main_image] => Array
                    (
                        [0] => http://example.com/user_adv/123.jpg
                        [1] => http://example.com/user_adv/124.jpg
                        [2] => http://example.com/user_adv/125.jpg
                    )

                [resize_image] => Array
                    (
                        [0] => http://example.com/user_adv/res/123.jpg
                        [1] => http://example.com/user_adv/res/124.jpg
                        [2] => http://example.com/user_adv/res/125.jpg
                    )

                [type] => user
            )

    )

[pinned_ads] => 0
[total_pages] => 240
[current_page] => 1
[total_ads_count] => 240
)

好的,現在我更新了代碼,但是在插入數據期間仍然遇到問題,我得到了一個錯誤

PHP Notice:  Undefined variable: string in /var/www/xx.php on line 36 

錯誤行是以$ query開頭,這是我的代碼

$result = curl_exec($ch);
$json = json_decode($result, true);

header('Content-Type: text/plain; charset=utf-8');


function mysqlconnect (){
global $db;
$db = mysqli_connect("localhost", "user_db","mypass","my_db");
if (!$db) {
  echo "Error: Could not connect to the database " . print_r(oci_error());
  exit;
    }
}

function mysqlclose () {
    global $db;
    mysqli_close($db);
}

  mysqlconnect();
 $query = "INSERT INTO wdwd VALUES (0,'" . $db->real_escape_string($string) . "')";

$result = $db->query($query);
mysqlclose();

print_r($json);

?>

確切知道您的數據庫表是什么樣子會很高興。

當您說需要將數據插入數據庫時​​,是指完整的json字符串嗎?

Just insert straight into a varchar,text,blob type.

或者您是說每個json字段代表表中的一列?

我會做Double H提到的。 通過使用返回數組

json_decode($output, true)

暫無
暫無

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

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