[ask] import text to mysql

Forum diskusi pemrograman web PHP

Moderators: irzan2010, trail

[ask] import text to mysql

Postby taufiq » 25 Jan 2012, 22:06

para master saya kebingungan untuk bikin import text to mysql lewat PHP, mohon bantuanya.

ilustrasi text yang akan saya import:
16# 216# 100503# M. Sobir
17# 217# 100504# Fatimah
18# 218# 100505# Rendi A.
19# 219# 100506# Bayu Okto S.
20# 220# 100507# Tuminah


----end of text----


ane sudah bikin koding nya:
Code: Select all
$file = "tmp/data.doc";
$fp = fopen($file, "r");
$data = fread($fp, filesize($file));
fclose($fp);
$output = str_replace("\t|\t", "#", $data);
$output = explode("\n", $output);
foreach($output as $var) {
$tmp = explode("#", $var);
$id = $tmp[0];
$nomor = $tmp[1];
$induk = $tmp[2];
$nama = $tmp [3];
$sql = "INSERT INTO data SET id='$id',
                      nomor='$nomor',
                      induk='$induk',
                      nama='$nama'";
mysql_query($sql);
}
echo "Done!";

datanya sudah bisa masuk
tapi untuk tulisan "-----end of text----" itu juga terbaca, jadi dimasukkan di tabel cuma tidak terbaca, jadi tabelnya kosong..

data yang ingin saya masukan adalah data yang diatas, kemudaian tulisan"----end of text----" tidak dimasukkan dalam tabel.
saya menyimpan text tersebut dalam extensi .doc
tanda # merupakan pemisah antar tiap atribut.

terimakasih para master atas bantuanya.. :D :D
taufiq
Prajurit Dua
Prajurit Dua
 
Posts: 25
Joined: 23 Jan 2012, 23:04
Memberi kopi: 15 cangkir
Mendapat kopi: 0 cangkir

Re: [ask] import text to mysql

Postby vberror13 » 27 Jan 2012, 10:07

Saya pikir lebih baik baca file textnya baris-per-baris saja. Dengan begitu bisa dilihat apakah baris yang dibaca memang merupakan baris yang akan dimasukkan ke dalam database. Contohnya seperti ini:
Code: Select all
    $file = "tmp/data.doc";
    $fp = fopen($file, "r");
   //-- read the line
   while ($line= fgets ($file)) {
      $tmp = explode("#",$line);
      if (count($tmp)==4)//arraynya harus sama dengan 4 element
      {
         $id = $tmp[0];
         $nomor = $tmp[1];
         $induk = $tmp[2];
         $nama = $tmp [3];
         $sql = "INSERT INTO data SET id='$id',
                          nomor='$nomor',
                          induk='$induk',
                          nama='$nama'";
         mysql_query($sql);
      }
   }
   $fclose($file);
   echo "Done!"; 

:ymbilly: :ymbilly: :ymbilly: =:) :ymbilly:
Just Because You Are Unique, Doesn't Mean You Are Useful

Post vberror13 telah mendapat kopi dari:
taufiq
User avatar
vberror13
Global Moderator
Global Moderator
 
Posts: 1847
Joined: 13 Mar 2010, 20:34
Location: Medan Indonesia
Memberi kopi: 296 cangkir
Mendapat kopi: 321 cangkir

Re: [ask] import text to mysql

Postby taufiq » 27 Jan 2012, 14:42

vberror13 wrote:Saya pikir lebih baik baca file textnya baris-per-baris saja. Dengan begitu bisa dilihat apakah baris yang dibaca memang merupakan baris yang akan dimasukkan ke dalam database. Contohnya seperti ini:
Code: Select all
    $file = "tmp/data.doc";
    $fp = fopen($file, "r");
   //-- read the line
   while ($line= fgets ($file)) {
      $tmp = explode("#",$line);
      if (count($tmp)==4)//arraynya harus sama dengan 4 element
      {
         $id = $tmp[0];
         $nomor = $tmp[1];
         $induk = $tmp[2];
         $nama = $tmp [3];
         $sql = "INSERT INTO data SET id='$id',
                          nomor='$nomor',
                          induk='$induk',
                          nama='$nama'";
         mysql_query($sql);
      }
   }
   $fclose($file);
   echo "Done!"; 



ko' blom bisa ya om...
ada peringatanya gini..
Warning: fgets(): supplied argument is not a valid stream resource in E:\xampp\htdocs\data\proses_import.php on line 12

Fatal error: Function name must be a string in E:\xampp\htdocs\data\proses_import.php on line 39


mohon penjelasanya lebih lanjut ya om... :(
taufiq
Prajurit Dua
Prajurit Dua
 
Posts: 25
Joined: 23 Jan 2012, 23:04
Memberi kopi: 15 cangkir
Mendapat kopi: 0 cangkir

Re: [ask] import text to mysql

Postby vberror13 » 27 Jan 2012, 16:16

ah, ya benar. Sorry, salah liat variabel . :D

Code: Select all

        $file = "tmp/data.doc";
        $fp = fopen($file, "r");
       //-- read the line
       while ($line= fgets ($fp)) {
          $tmp = explode("#",$line);
          if (count($tmp)==4)//arraynya harus sama dengan 4 element
          {
             $id = $tmp[0];
             $nomor = $tmp[1];
             $induk = $tmp[2];
             $nama = $tmp [3];
             $sql = "INSERT INTO data SET id='$id',
                              nomor='$nomor',
                              induk='$induk',
                              nama='$nama'";
             mysql_query($sql);
          }
       }
       fclose($fp);
       echo "Done!";


:ymbilly: :ymbilly: :ymbilly: =:) :ymbilly:
Just Because You Are Unique, Doesn't Mean You Are Useful

Post vberror13 telah mendapat kopi dari:
taufiq
User avatar
vberror13
Global Moderator
Global Moderator
 
Posts: 1847
Joined: 13 Mar 2010, 20:34
Location: Medan Indonesia
Memberi kopi: 296 cangkir
Mendapat kopi: 321 cangkir

Re: [ask] import text to mysql

Postby taufiq » 27 Jan 2012, 20:44

vberror13 wrote:ah, ya benar. Sorry, salah liat variabel . :D

Code: Select all

        $file = "tmp/data.doc";
        $fp = fopen($file, "r");
       //-- read the line
       while ($line= fgets ($fp)) {
          $tmp = explode("#",$line);
          if (count($tmp)==4)//arraynya harus sama dengan 4 element
          {
             $id = $tmp[0];
             $nomor = $tmp[1];
             $induk = $tmp[2];
             $nama = $tmp [3];
             $sql = "INSERT INTO data SET id='$id',
                              nomor='$nomor',
                              induk='$induk',
                              nama='$nama'";
             mysql_query($sql);
          }
       }
       fclose($fp);
       echo "Done!";




mantab om...terimaksih banyak bantuanya.. :D :D
taufiq
Prajurit Dua
Prajurit Dua
 
Posts: 25
Joined: 23 Jan 2012, 23:04
Memberi kopi: 15 cangkir
Mendapat kopi: 0 cangkir


Return to PHP

Who is online

Users browsing this forum: No registered users and 4 guests