GWibisono

step 2.. insert dari json.

Nov 1st, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.87 KB | None | 0 0
  1. <?php
  2. $database="test";
  3.  
  4. $user = 'root';
  5. $password = '';
  6. $dsn = 'mysql:dbname='.$database.';host=localhost';
  7.  
  8. try {
  9.     $conn = new PDO($dsn, $user, $password);
  10. } catch (PDOException $e) {
  11.     echo 'Connection failed: ' . $e->getMessage();
  12. }
  13.  
  14. $d = dir("data");
  15.  
  16. while (false !== ($entry = $d->read()))
  17. {
  18.     if($entry!='.'&&$entry!='..')
  19.     {
  20.        //echo $entry."\n<br/>";
  21.        $f=file_get_contents("data/$entry");
  22.        $ar=json_decode($f, TRUE);
  23.        $table=str_replace(".json",'',$entry);
  24.        $table=trim($table);
  25.        
  26.        foreach($ar as $ar2)
  27.        {
  28.         $data="";$i=1;
  29.             foreach($ar2 as $val)
  30.             {
  31.                 if($i!=1)
  32.                 {
  33.                     $data.=", ";
  34.                 }
  35.                 $i++;
  36.                 $data.="'".addslashes($val)."'";
  37.            }
  38.            $sql="insert into $table values ($data);";
  39.            $sth = $conn->prepare($sql);
  40.             $sth->execute();
  41.            $data='';
  42.        }
  43.    }
  44. }
  45. $d->close();
  46.  
  47. echo 'done';
Advertisement
Add Comment
Please, Sign In to add comment