Пример #1
0
 public function import_person_file_to_db()
 {
     //Import to a DB the data of a file
     $dbinfo = new db_connection();
     $file = fopen("personinfo.txt", "r") or die("ERROR: Fail reading the file");
     try {
         $conn = new PDO("mysql:host=localhost;dbname=myowndb", $dbinfo->get_dbusr(), $dbinfo->get_dbpw());
         // set the PDO error mode to exception
         $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         //echo "Connected successfully<br><br>";
         while (!feof($file)) {
             $line = fgets($file);
             $datarray = explode(";", $line);
             /*if( !isset($datarray[0]) && !isset($datarray[1]) && !isset($datarray[2]) && 
             		!isset($datarray[3]) && !isset($datarray[4]) && !isset($datarray[5]) && !isset($datarray[6]) ) {
             					$datarray[0] = null;
             					$datarray[1] = null;
             					$datarray[2] = null;
             					$datarray[3] = null;
             					$datarray[4] = null;
             					$datarray[5] = null;
             					$datarray[6] = null;
             				}*/
             $sql = "INSERT INTO person (firstname, lastname, id, age, gender, phone, email) VALUES ('{$datarray['0']}', '{$datarray['1']}', '{$datarray['2']}', '{$datarray['3']}', '{$datarray['4']}', '{$datarray['5']}', '{$datarray['6']}')";
             $conn->exec($sql);
         }
     } catch (PDOException $e) {
         echo "Connection failed: " . $e->getMessage();
     }
 }