jassim mishal
jassim mishal

Reputation: 83

Insert json data into mysql table

I know parsing json data has been discussed lots but what I want is probably a little simpler

I need a little editing to the bleow php script to work as convert json data and push it into MySQL table since the script work as reading the json data only! I'm not much familiar with php coding.

Any help is appreciated in advance.

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

?>

and the result i got

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
)

ok now I've update the code but still face issue during inserting the data i got an error which are

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

the error line is that start with $query and here my code

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

?>

Upvotes: 1

Views: 2004

Answers (1)

slik
slik

Reputation: 5221

It would be nice to know exactly what your database table looks like.

When you say you need to insert data into the database are you referring to the full json string?

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

Or do you mean each json field represents a column in the table?

I would do what Double H mentioned. Return an array by using

json_decode($output, true).

Upvotes: 1

Related Questions