Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Insert json data into mysql table, problem

php json

This topic has been archived. This means that you cannot reply to this topic.
No replies to this topic

#1 jmishal

jmishal

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 27 November 2015 - 05:49 PM

Hi,
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.

    <?
    $item_string = '{"para":{"Psize":"1","Doffset":"now","Did":"33","lang":"en","p":1,"tkn":"cls","subctegry":"14"},"req":"adv\/get_adv"}';
    $ch = curl_init('http://example.com/webd/');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $item_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($item_string))
    );
    $result = curl_exec($ch);
    $item = json_decode($result, true);
     //Database Connection
    mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("sale") or die(mysql_error());
    mysql_query("INSERT INTO wdwd VALUES ('".$item['is_fav']."','".$item['is_new']."','".$item['description']."','".$item['is_sold']."','".$item['language']."','".$item['image']."','".$item['contact_no']."','".$item['is_pinned']."','".$item['user_adv_id']."','".$item['premium_tag']."','".$item['keywords']."','".$item['title']."','".$item['is_not_abusive']."','".$item['announce_date']."','".$item['user_id']."','".$item['price']."','".$item['pinned_ads']."','".$item['total_pages']."','".$item['current_page']."','".$item['total_ads_count']."')");
         mysql_close();
         echo "Data Berhasil Disimpan...";
         //print out
     #header('Content-Type: text/plain; charset=utf-8');
    print_r($item);
    ?>

and here i got ERROR with the output .. and also i've check the database rows it's empty!! :(

    PHP Notice:  Undefined index: is_fav in /var/www/db/xcall.php on line 22
    PHP Notice:  Undefined index: is_new in /var/www/db/xcall.php on line 22
    PHP Notice:  Undefined index: description in /var/www/db/xcall.php on line 22
    PHP Notice:  Undefined index: is_sold in /var/www/db/xcall.php on line 22
    PHP Notice:  Undefined index: language in /var/www/db/xcall.php on line 22
    PHP Notice:  Undefined index: image in /var/www/db/xcall.php on line 22
    PHP Notice:  Undefined index: contact_no in /var/www/db/xcall.php on line 22
    PHP Notice:  Undefined index: is_pinned in /var/www/db/xcall.php on line 22
    PHP Notice:  Undefined index: user_adv_id in /var/www/db/xcall.php on line 22
    PHP Notice:  Undefined index: premium_tag in /var/www/db/xcall.php on line 22
    PHP Notice:  Undefined index: keywords in /var/www/db/xcall.php on line 22
    PHP Notice:  Undefined index: title in /var/www/db/xcall.php on line 22
    PHP Notice:  Undefined index: is_not_abusive in /var/www/db/xcall.php on line 22
    PHP Notice:  Undefined index: announce_date in /var/www/db/xcall.php on line 22
    PHP Notice:  Undefined index: user_id in /var/www/db/xcall.php on line 22
    PHP Notice:  Undefined index: price in /var/www/db/xcall.php on line 22
    Array
    (
        [data] => Array
            (
                [0] => Array
                    (
                        [is_fav] => 0
                        [is_new] => 1
                        [description] =>  blablabla
                        [is_sold] => 0
                        [language] => en
                        [image] => 
                        [contact_no] => +10000000
                        [is_pinned] => 1
                        [user_adv_id] => 234499
                        [premium_tag] => 1
                        [keywords] => 
                        [title] => oblabla 
                        [is_not_abusive] => 0
                        [announce_date] => 2012-03-01 19:10:16
                        [user_id] => 99221
                        [price] => 10
                        [main_image] => Array
                            (
                                [0] => http://example.com/images/12.jpg
                                [1] => http://example.com/images/13.jpg
                            )
                        [resize_image] => Array
                            (
                                [0] => http://example.com/images/resize/12.jpg
                                [1] => http://example.com/images/resize/13.jpg
                            )
                        [type] => user
                    )
                [1] => Array
                    (
                        [is_fav] => 0
                        [is_new] => 1
                        [description] => blabla vblakdf
                        [is_sold] => 0
                        [language] => en
                        [image] => 
                        [contact_no] => +199443288
                        [is_pinned] => 0
                        [user_adv_id] => 1234238
                        [premium_tag] => 0
                        [keywords] => 
                        [title] =>  groupx for sale
                        [is_not_abusive] => 0
                        [announce_date] => 2012-01-28 00:08:10
                        [user_id] => 123
                        [price] => 000
                        [main_image] => Array
                            (
                                [0] => http://example.com/images/no-pic.png
                            )
                        [resize_image] => Array
                            (
                                [0] => http://example.com/images/resize/no-pic.png
                            )
                        [type] => user
                    )
        )

    [pinned_ads] => 2
    [total_pages] => 361
    [current_page] => 1
    [total_ads_count] => 361
)

and here my database table

    mysql> desc wdwd;
    +-----------------+-------------+------+-----+---------+-------+
    | Field           | Type        | Null | Key | Default | Extra |
    +-----------------+-------------+------+-----+---------+-------+
    | id              | int(11)     | YES  |     | NULL    |       |
    | is_fav          | varchar(20) | YES  |     | NULL    |       |
    | is_new          | varchar(20) | YES  |     | NULL    |       |
    | description     | varchar(20) | YES  |     | NULL    |       |
    | is_sold         | varchar(20) | YES  |     | NULL    |       |
    | language        | varchar(20) | YES  |     | NULL    |       |
    | image           | varchar(20) | YES  |     | NULL    |       |
    | contact_no      | varchar(20) | YES  |     | NULL    |       |
    | is_pinned       | varchar(20) | YES  |     | NULL    |       |
    | user_adv_id     | varchar(20) | YES  |     | NULL    |       |
    | premium_tag     | varchar(20) | YES  |     | NULL    |       |
    | keywords        | varchar(20) | YES  |     | NULL    |       |
    | title           | varchar(20) | YES  |     | NULL    |       |
    | is_not_abusive  | varchar(20) | YES  |     | NULL    |       |
    | announce_date   | varchar(20) | YES  |     | NULL    |       |
    | user_id         | varchar(20) | YES  |     | NULL    |       |
    | price           | varchar(20) | YES  |     | NULL    |       |
    | main_image      | varchar(20) | YES  |     | NULL    |       |
    | resize_image    | varchar(20) | YES  |     | NULL    |       |
    | type            | varchar(20) | YES  |     | NULL    |       |
    | pinned_ads      | varchar(20) | YES  |     | NULL    |       |
    | total_pages     | varchar(20) | YES  |     | NULL    |       |
    | current_page    | varchar(20) | YES  |     | NULL    |       |
    | total_ads_count | varchar(20) | YES  |     | NULL    |       |
    +-----------------+-------------+------+-----+---------+-------+
any help on this would be appreciated-

Thanks!


Edited by jmishal, 27 November 2015 - 05:51 PM.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download