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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.