Using PHP 4.3.7 and PGSQL 7.4.3, I am trying to structure data and use
COPY to import into a table using PHP. If I run my script, I receive
this error:

Warning: pg_end_copy(): Query failed: in
/home/sites/site9/web/importxrf.php on line 100

I view source of the page where I have echo'd the copy data and paste it
into my psql prompt and it copies into the table fine. Can anyone see
any obvious problems with my script?

    $connOHC = "dbname=ohc user=xxxx password=xxxxx host=example.com";
        $dbh = pg_connect($connOHC);
        if ($dbh) {echo "Connection to database established...<br>";}
        $stat = pg_exec($dbh, "SELECT MAX(public.tblxrf.xrf_id) AS units_max_id
FROM public.tblxrf");
        if ($stat) {
                $data = pg_fetch_row($stat,0);
                $next_id = $data[0]+1;
        } else {
                $next_id = 1;
        }
        echo "Starting process...<br>";
        echo "Begin: ".date('Y-m-d H:i:s')."<br>\n";
        $result = pg_exec($dbh, "COPY tblxrf FROM stdin");

        $handle = fopen("xrf/".$recNo."__xln", "r") or die("Can't open file");
        $lineno = 0;
        $success = true;
        while ($csv_line = fgetcsv($handle,1024)) {
                $lineno++;
                chop($csv_line);
                switch ($lineno) {
                        case (($lineno > 2) && ($lineno < 6)):
                                break;
                        case 8:
                                break;
                        case 1:
                                if (!preg_match("/^Serial/",$csv_line[0])) {
                                        echo "Not a valid XRF file, no Serial number 
found\n";
                                        $success = false;
                                        break 2;
                                } else { $serialno = $csv_line[0]; };
                                break;
                        case 2:
                                if (!preg_match("/^PAINT/",$csv_line[0])) {
                                        echo "Not a valid XRF paint file, PAINT not 
found\n";
                                        $success = false;
                                        break 2;
                                }
                                break;
                                break 2;
                        case ($lineno >= 10):
                                $copydata = $next_id.",";
                                for ($i = 0, $j = count($csv_line); $i < $j; $i++) {
                                        $trimdata = trim($csv_line[$i]);
                                        if ($i <= 25) {
                                                if (($trimdata == "") || ($trimdata == 
"NA")) {
                                                        switch ($i) {
                                                                case 25:        
$copydata .= "\\N";
                                                                                       
 break;
                                                                default:        
$copydata .= "\\N,";
                                                                                       
 break;
                                                        }
                                                } elseif ($trimdata == ">>5.0") {
                                                        $copydata .= "\\N,";
                                                } else {
                                                        $copydata .= $trimdata.",";
                                                }
                                        }
                                }
                                $copydata .= "\n";
                                $stat = pg_put_line($dbh, $copydata);
                                if (!$stat) {
                                        echo "An error has occured<br>\n";
                                        exit;
                                }
                                echo $copydata;
                                $next_id++;
                                break;
                } // end switch
        } // end while
        pg_put_line($dbh, "\\.\n");
        pg_end_copy($dbh);
        echo "End: ".date('Y-m-d H:i:s')."<br>\n";
        pg_close($dbh);
        echo "ending process...<br>\n";
        fclose($handle);

-- 
Robert

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to