Re: postgresql csv import pdo

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


On 14/09/2021 22:50, ourdiaspora wrote:

Forgive the simplicity, but having problems to start:

	try {
		$dbconnect = new PDO(pgsql:host=localhost; dbname=cpacweb; user=someuser; password=somepassword);
	catch(PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";

	$query = "SELECT * FROM somedata";
	$result = pg_exec($dbconnect, $query);
	echo "Number of rows: " . pg_numrows($result);

No error message, no print of rows of table. Please what is the error?

I can see 2 obvious issues with this code:

1) The DSN passed to PDO should be a single string, but this is missing quotes in the code above;

2) You are mixing PDO with pg_ functions - this will not work as they are separate extensions. You need to use only 1 or the other - either use PDO, or use pg_ functions.

If you're using PDO on PHP < 8, you'll want to enable the exceptions error reporting mode. See (In PHP 8+ the default PDO error reporting mode is already set to exceptions)

For general error reporting in PHP, ensure that the following ini settings are set:

* error_reporting = E_ALL  (report all errors and warnings)
* display_errors = 1  (display errors in output / on the web page)

On production you'll want to set display_errors to 0 and use the log_errors and error_log ini settings to log errors to a file instead as some error messages may contain sensitive information such as passwords or api keys.

For more information on these ini settings see:

[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