将json数据插入mysql表

时间:2015-07-04 02:43:52

标签: php mysql json

我知道解析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);

?>

1 个答案:

答案 0 :(得分:1)

确切地知道数据库表的样子会很好。

当您说需要将数据插入数据库时​​,您是指完整的json字符串吗?

if ($gender == 'male') $src = '/images/male.png'; else $src = '/images/female.png'; echo sprintf("<img alt=\"avatar\" src=\"%s\" />", $src);

或者你的意思是每个json字段代表表中的一列?

我会做Double H提到的。使用

返回一个数组

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