Re: postgresql csv import

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

 



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);

should be inside the where-loop braces, not outside…
You code does no error checking - what is the value of $result after this?

It may be that there is no ‘error’ as such. The INSERT runs correctly for what you have asked it to do unless there is some database constraint on the value in that table like the id or email should be unique. 
Whatever the SELECT statement to get the number of rows returns it what was inserted - I suspect that one row should be added to the table every time the code is run, and it will be values from the last row of the CSV.
Is this what you observe?

When asking for help, it always helps if you explain what you actually observe happening with your code, and not just the thing you expected to happen, or what didn’t happen. Logs are not everything - they only contain what was reported and often only actual errors, not successful (but wrong) operations.
Use the database tools available outside of PHP (e.g. phpPgAdmin, pgAdmin, even just the psql command-line) to check what the database table contains after the code runs. If you don’t know how to do this then you may not yet be ready to write database applications. If you do know how to do this, you could show us the results…

Pete

On 19 Sep 2021, at 18:49, ourdiaspora <ourdiaspora@xxxxxxxxxxxxxx> wrote:


On Thursday, September 16th, 2021 at 9:30 PM, ourdiaspora <ourdiaspora@xxxxxxxxxxxxxx> wrote:

sudo nano /etc/php/7.0/apache2/php.ini

"

...

[line number]483 log_errors = On

...

571 error_log = ~/tmp/webserver/phperrors.log

...

The directory does not show creation of such an error file to interrogate further. Please what next to investigate?

Readers,

Continuation of failures occurs:

<?php
$uploaddir = '~/tmp/cpac/cpactmp/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$dbconnect = pg_connect("dbname=cpacweb user=cpaca");
$targetfilepath = $uploaddir . $uploadfile;
if(isset($_POST["submit"])){
$filename=$_FILES["userfile"]["tmp_name"];
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);
}
$query = "SELECT * FROM somedata";
$result = pg_exec($dbconnect, $query);
echo "Number of rows: " . pg_numrows($result);
pg_freeresult($result);
pg_close($dbconnect);
?>

Connection to the database is successful, but not csv import and subsequent data entry.

As posted previous, also do not understand why error logs are not being produced. Please what to do?

The file 'phpinfo()' shows:
"
...
error_log -1 -1
...
"

The error log is not produced.


[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