Parse JSON Insert Database

Source: Internet
Author: User
Tags rtrim

<?PHPHeader("Content-type:text/html;charset=utf-8");include_once('./mysql.php ');$filename= "F:/heyu.txt";$handle=fopen($filename, "R");//when reading a binary file, you need to set the second parameter to ' RB '//To get the file size by filesize, and read the entire file into a string$contents=fread($handle,filesize($filename));$arr= Json_decode ($contents,true);$arr=$arr[' Weibosearchcontent '];$str _target= ' ';$str _data= ' ';$conndb=Newconndb ();foreach($arr  as $i=$msg){    Echo $i.‘ <br> '; foreach($msg  as $key=$val){            //echo $key. ' = '. $val. ' <br> ';            $str _target.=$key.‘,‘; if($key= = ' UID '){                $val=Str_replace(' 1001030103 ', ',$val); }                        if($key= = ' titles ') {//traversing the moods array                                $tmp= ' '; foreach($val  as $t=$v){                        $tmp.=$v.‘,‘; }                    $tmp=RTrim($tmp,","); $str _data. = ' \ '.$tmp.‘ \‘,‘; }            Else if($key= = ' pics ') {//traversing the moods array                                $tmp= ' '; foreach($val  as $t=$v){                        $tmp.=$v.‘,‘; }                    $tmp=RTrim($tmp,","); $str _data. = ' \ '.$tmp.‘ \‘,‘; }            Else if($key= = ' moods ') {//traversing the moods array                                $tmp= ' '; foreach($val  as $t=$v){                        $tmp.=$v.‘,‘; }                    $tmp=RTrim($tmp,","); $str _data. = ' \ '.$tmp.‘ \‘,‘; }            Else{                $str _data. = ' \ '.$val.‘ \‘,‘; }                }        //remove the last comma        $str _target=RTrim($str _target, ","); $str _data=RTrim($str _data, ","); Echo $str _target.‘ <br> '; Echo $str _data.‘ <br> '; $sql= ' INSERT into Keyword_weibo ('.$str _target.‘) VALUES ('.$str _data.‘)‘; $conndb->query ($sql); $str _target= ' '; $str _data= ' '; }fclose($handle);?>

Parse JSON Insert Database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.