RTC222
RTC222

Reputation: 2323

PHP PDO for Postgres not enabled even though enabled in php.ini file

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.

Upvotes: 2

Views: 4243

Answers (1)

Jorge
Jorge

Reputation: 91

You should try to comment back those extensions in php.ini file. It worked for me. Read more about in this post

I had the same difficulty. I am using Debian 10, PostgreSQL 12, Apache 2.4.38 and php7.3. Following the directions found on most sites:

1-I installed php

apt-get install php libapache2-mod-php

When trying to run my application I got Internal Server Error 500

2- I edited the php.ini file, uncommenting the extensions pdo_pgsql and pgsql and restarting the server

But the same error was repeated: Internal Server Error 500

3-I installed the pgsql package from php

apt-get install php7.3-pgsql
apachectl restart

After this step the error 500 was solved, but the two warnings mentioned by RTC222 appeared. Which by the way I only saw when I took a look at the php error.log file.

To solve the problem I did several searches that did not solve anything, until I found the aforementioned blog, which explains that the cause of this is (and I quote):

"There are two ways to load most extensions in PHP. One is by compiling the extension directly into the PHP binary. The other is by loading a shared extension dynamically via an ini file. The errors indicate that dynamic extensions are being loaded via .ini files, even though they are already compiled into the PHP binary. "

4- I uncommented the extensions of interest in the php.ini file and restarting the server everything was solved satisfactorily.

I hope this help someone.

Upvotes: 1

Related Questions