Insert JSON data in MySQL database with PHP -


i tried parse json file using php. put extracted data in mysql database.

this json file

    {"u":[         {"text":"chef salad calling name, i\u0027m hungry!",         "id_str":"28965131362770944",         "created_at":"dom gen 23 24:00:00 +0000 2011",         "user":             {"id_str":"27144739",             "screen_name":"lovelythang80",             "name":"one of kind"}},          {....} //other same code     } 

and php code

<?php      //connect mysql db      //read json file contents      //convert json object php associative array     $data = json_decode($jsondata, true);      //prepare variables insert     $sqlu = "insert user(id_user, screen_name, name)     values ";     $sqlt = "insert tweet(id_user, date, id_tweet, text)     values ";      //i analyze whole array , assign values variables     foreach ($data $u => $z){         foreach ($z $n => $line){             foreach ($line $key => $value) {                  switch ($key){                     case 'text':                         $text = $value;                         break;                     case 'id_str':                         $id_tweet = $value;                         break;                     case 'created_at':                         $date = $value;                         break;                     case 'user':                         foreach ($value $k => $v) {                             switch($k){                                 case 'id_str':                                     $id_user = $v;                                     break;                                 case 'screen_name':                                     $screen_name = $v;                                     break;                                 case 'name':                                     $name = $v;                                     break;                             }                         }                         //insert value table                         $sqlu .= "('".$id_user."', '".$screen_name."', '".$name."')";                         break;                 }             }             //insert value table             $sqlt .= "('".$id_user."', '".$date."', '".$id_tweet."', '".$text."')";         }     }  ?> 

but in table doesn't enter anything!

and if try:

echo $sqlu; 

output:

insert user(id_user, screen_name, name)  values ('27144739', 'lovelythang80', 'one of kind') insert user(id_user, screen_name, name)  values ('27144739', 'lovelythang80', 'one of kind')('21533938', 'sherekhan77', 'loki camina cielos ') insert user(id_user, screen_name, name)  values ('27144739', 'lovelythang80', 'one of kind')('21533938', 'sherekhan77', 'loki camina cielos ')('45378162', 'cosmic_dog', 'pablo') 

and same

echo $sqlt; 

why?

you forgot add , between values:

insert user(id_user, screen_name, name) values ('27144739', 'lovelythang80', 'one of kind'), ('21533938', 'sherekhan77', 'loki camina cielos ')

and can insert one insert statement @ once.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -