I am inserting records into a Postgres database table using PDO with named placeholders, but I get an error. I did not have PHP error logging turned on, so I enabled that, to go into php_errors.log (I created that new log file).
The new error log shows only two startup warnings:
[13-Aug-2019 17:26:08 UTC] PHP Warning: PHP Startup: Unable to load dynamic library 'pdo_pgsql' (tried: /usr/lib/php/20180731/pdo_pgsql (/usr/lib/php/20180731/pdo_pgsql: cannot open shared object file: No such file or directory), /usr/lib/php/20180731/pdo_pgsql.so (/usr/lib/php/20180731/pdo_pgsql.so: undefined symbol: pdo_parse_params)) in Unknown on line 0
[13-Aug-2019 17:26:08 UTC] PHP Warning: Module 'pgsql' already loaded in Unknown on line 0
The file /usr/lib/php/20180731/pdo_pgsql.so is there, despite what the warning says. In my php.ini file, the following lines are uncommented:
extension=pdo_pgsql
extension=pgsql
I thought that was all I need to do to enable pdo. However, in my phpinfo web page there is no entry for pdo, as there should be, so it looks like pdo is not there, even though pgsql works at least to log onto a Postgres database.
I do know that php is working with Postgres because when I run the script without the database insertion code (just logging on to Postgres), the dev console shows a success message (“successfully connected to database”).
My question is: what else do I need to do to enable pdo?
Here is the full script file, although it may not be needed for this question. As I said, without the try-catch block below, the dev console reports a successful logon to the Postgres database, but when I enable to try-catch block with the pdo record insertion code, it fails, apparently because pdo is not enabled.
<?php
echo 'Hello ' . htmlspecialchars($_POST["firstname"]) . '!' ;
$dsn = vsprintf('pgsql:host=%s;port=%s;dbname=%s;user=%s;password=%s', [
'host' => 'xx.xxx.xxx.xxx',
'port' => '5432',
'dbname' => 'dbname',
'user' => 'username',
'password' => 'password',
]);
echo $dsn;
echo PHP_EOL;
try{
// create a PostgreSQL database connection
$conn = new PDO($dsn);
// display a message if connected to the PostgreSQL successfully
if($conn){
echo "Connected to the database successfully!";
}
}catch (PDOException $e){
// report error message
echo $e->getMessage();
}
$fields = array('date', 'email', 'firstname', 'lastname', 'password', 'comments', 'sendupdates');
$fieldlist = implode(',', $fields);
echo $fieldlist;
echo PHP_EOL;
$datefield = $_POST['datefield'];
$email_field = $_POST['email_field'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$password = $_POST['password'];
$comments = $_POST['comments'];
$custom_checkbox = $_POST['custom_checkbox'];
echo $datefield;
echo PHP_EOL;
echo $email_field;
echo PHP_EOL;
echo $firstname;
echo PHP_EOL;
echo $lastname;
echo PHP_EOL;
echo $password;
echo PHP_EOL;
echo $comments;
echo PHP_EOL;
echo $custom_checkbox;
echo PHP_EOL;
try {
$datefield = $_POST['datefield'];
$email_field = $_POST['email_field'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$password = $_POST['password'];
$comments = $_POST['comments'];
$custom_checkbox = $_POST['custom_checkbox'];
$data = [
'date' => $datefield,
'email' => $email_field,
'firstname' => $firstname,
'lastname' => $lastname,
'password' => $password,
'comments' => $password,
'checkbox' => $custom_checkbox,
];
$sql = "INSERT INTO psq01 (date, email, firstname, lastname, password, comments, sendupdates) VALUES (:date, :email, :firstname, :lastname, :password, :comments, :checkbox)";
$stmt= $pdo->prepare($sql);
$stmt->execute($data);
} catch (PDOException $e) {
error_log($e->getMessage());
}
$conn = null;
?>
So again, what else do I need to enable PDO? I'm using Ubuntu 18.04.
Thanks for any help with this.