Re: postgresql csv import

From: Date: Mon, 20 Sep 2021 21:14:35 +0000
Subject: Re: postgresql csv import
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-327714@lists.php.net to get a copy of this message
‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐ On Monday, September 20th, 2021 at 10:51 AM, Peter Ford <pete@justcroft.com> 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 ...).

« previous php.general (#327714) next »