1

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

?>
3
  • This is a very common task, could I suggest you google the tags you used here? If you get stuck I'd be happy to help but I know for a fact that there are an endless number of better resources already on the web which are better than myself. I will add that you could add that you could add 'mysqli' to your search. Parametrized queries are the preferred method for things like this. Commented Jul 4, 2015 at 2:58
  • You can insert the encoded json string to the mysql table. Set the datatype as text. Commented Jul 4, 2015 at 2:58
  • Use json_decode($output,true); to return as an array and insert it in your database Commented Jul 4, 2015 at 3:10

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.