Re: postgresql csv import

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Monday, September 20th, 2021 at 10:51 AM, Peter Ford <pete@xxxxxxxxxxxxx> wrote:

> I’m looking at the loop structure here and I see that the variable $dbdataentry is set to hold an SQL INSERT statement for each line of the CSV that is read, but the statement is only used OUTSIDE of the loop - that is, only the last line of the CSV would ever be inserted using this code.
> In plain terms, I think the line
>
> $result = pg_query($dbconnect, $dbdataentry);
>

The file was edited to:
"
...
if($_FILES["file"]["size"] > 0){
	$file = fopen($filename, "r");
	while (($getdata = fgetcsv($file, 10000, ",")) !== FALSE){
		$dbdataentry = "INSERT INTO somedata(name, id, emailaddress, groupname) VALUES ('".$getdata[0]."','".$getdata[1]."','".$getdata[2]."','".$getdata[3]."')";
		$result = pg_query($dbconnect, $dbdataentry);
		}
	}
	fclose($file);
 }
...
"

Neither the last line of the csv file, nor the entirety of the csv file, was inserted successfully into the database.

> You code does no error checking - what is the value of $result after this?
>

The result is zero (0) rows in the database.

The test csv file contains two rows of data, neither rows inserted.
The postgreqsl command 'psql' is used to see if any data was inserted (i.e. SELECT * FROM ...). Data could be entered into the database from the postgresql command terminal (i.e. INSERT INTO ...).




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux