Re: postgresql csv import

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

 




On 20/09/2021 22:14, ourdiaspora wrote:
‐‐‐‐‐‐‐ 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 ...).

So what exactly is your code doing? Check each part:

1. Is the uploaded file actually detected with a size over 0 bytes?
2. Are you able to successfully open it for reading?
3. Is it even a valid CSV that can be read with fgetcsv() ?
4. Does it loop over the data the expected number of times?
5. Is the CSV data constructed in the right way that your $getdata indexes are correct when you build the query?

This thread has been going on a long while, with you continually posting code and repeating variations of "this doesn't work, why?". There are lots of reasons it might not work, but you need to start digging into your own code and giving a bit more information if you want useful feedback.

A very basic method of debugging is to insert simple print statements into code at various points to determine that it reached that part of the code and reached it with the expected values.

--
Ashley Sheridan
https://www.ashleysheridan.co.uk



[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