On 14/09/2021 22:50, ourdiaspora wrote:
Readers,
Forgive the simplicity, but having problems to start:
<?php
try {
$dbconnect = new PDO(pgsql:host=localhost; dbname=cpacweb; user=someuser; password=somepassword);
}
catch(PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
$query = "SELECT * FROM somedata";
$result = pg_exec($dbconnect, $query);
echo "Number of rows: " . pg_numrows($result);
pg_freeresult($result);
pg_close($dbconnect);
?>
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 https://phpdelusions.net/pdo#errors
(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:
https://www.php.net/manual/en/errorfunc.configuration.php