Jump to content

saving read txtfile data to db help


sasori

Recommended Posts

hi, i have the class here, and am trying to save the read column of the textfile to db. but unfortunately it's not being save, i don't know what's wrong... I also attached the textfile so you'll have an idea what am i trying to read from

 

class Extractor
{
  public $filehandler;
  public $data = array();

  // set your db access details
  private $host = HOST;
  private $username = USERNAME;
  private $password = PASSWORD;
  private $dbname = DBNAME;



  public function __construct($file)
  {
    $this->filehandler = $file;
  }

  //prints col1 of textfile
  public function printcol1()
  {
    $fp = fopen($this->filehandler,'r');
    while(($this->data = fgetcsv($fp,1000,",")) !== FALSE)
    {
       # $this->data[0]."<br />";
        $this->savetodb($this->data[0]);
    }
    fclose($fp);
  }

  public function savetodb($data)
  {
    try{
      $pdo = new PDO("mysql:host=$this->host;dbname=$this->dbname",$this->username,$this->password);
      $pdo->exec("INSERT INTO test(col,id) VALUES ({$data},null)");
      $pdo = null;
    }catch(PDOException $e){
      echo $e->getMessage()."<br />";
    }
  }

}

$file = 'iso3166.txt';
$shit = new Extractor($file);
$shit->printcol1();

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/215908-saving-read-txtfile-data-to-db-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.