0

I am doing inserts via a loop, unfortunately, it seems to insert only a few of the data and ignore some.

I'm reading the contents of a file and inserting them into a postgres database using PHP.

See my code below.

$source='/Users/gsarfo/AVEC_ETL/TCCDec052016OSU.DAT';

$lines=file($source);

$len =sizeof($lines);

$connec = new PDO("pgsql:host=$dbhost;dbname=$dbname", $dbuser,    $dbpwd); 

$ins=$connec->query('truncate table tbl_naaccr_staging');

try {

    for ($x = 0; $x < $len; $x++) {
        $a1=substr($lines[$x],146,9);
        $a2=substr($lines[$x],2182,9);
        $a3=substr($lines[$x],192,3);
        $connec->beginTransaction();

        $sql2=$connec->prepare("INSERT INTO tbl_naaccr_staging
                                (addr,name, email) VALUES (?, ?, ?"); 

        $sql2->execute(array($a1,   $a2,    $a3));
        $connec->commit();     
    }
    $res=$connec->query($sql) ;
}

catch (PDOException $e) { 
    echo "Error : " . $e->getMessage() . "<br/>"; 
    die(); 
} 

if ($sql2)
{echo 'success';}
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149

2 Answers2

1

I dont see how that would be inserting anything!

This line is incorrect

$sql2=$connec->prepare("INSERT INTO tbl_naaccr_staging
                       (addr,name, email) VALUES (?, ?, ?"); 
                                                         ^ ^ here

Correct it to

$sql2=$connec->prepare("INSERT INTO tbl_naaccr_staging
                       (addr,name, email) VALUES (?, ?, ?)"); 

Also your transaction does not make much sense as it commits every update, which is what would happen if you did not start a transaction. So maybe this would be sensible, and will achieve an all or nothing senario

In addition, a prepare can be reused many time, so also move that out of the loop, you will find your script runs quicker as well.

try {

    $connec->beginTransaction();   

    // move this out of the loop
    $sql2=$connec->prepare("INSERT INTO tbl_naaccr_staging
                            (addr,name, email) VALUES (?, ?, ?)");  

    for ($x = 0; $x < $len; $x++) {
        $a1=substr($lines[$x],146,9);
        $a2=substr($lines[$x],2182,9);
        $a3=substr($lines[$x],192,3);

        $sql2->execute(array($a1,   $a2,    $a3));
    }
    $connec->commit();  

    // I do not see a `$sql` variable so this query seems to have no function
    //$res=$connec->query($sql) ;
}

catch (PDOException $e) { 
    $connec->rollback();     

    echo "Error : " . $e->getMessage() . "<br/>"; 
    die(); 
} 
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

It worked, problem was due to unescaped characters in the strings being inserted so pg_escape_string helped clean out the string prior to insert