Postgres的php pdo:“找不到驱动程序"; [英] php pdo for Postgres: "could not find driver"

查看:105
本文介绍了Postgres的php pdo:“找不到驱动程序";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用于Postgres的新Ubuntu 18.04服务器上安装了php,但是php存在问题.这是我的php 7.3安装步骤:

I installed php on a new Ubuntu 18.04 server for Postgres, and I have a problem with php. Here are my php 7.3 installation steps:

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php]
sudo systemctl restart apache2

在安装PHP 7.3之后,我安装了pgsql.

After installing PHP 7.3, I installed pgsql.

sudo apt install php-pgsql
sudo service apache2 reload

接下来,我在/etc/php/7.3/apache2中编辑了php.ini文件,并从以下几行中删除了分号:

Next I edited the php.ini file in /etc/php/7.3/apache2 and removed the semi-colons from the following lines:

extension=pdo_pgsql
extension=pgsql

我保存了文件,然后sudo systemctl重新启动了apache2.

I saved the file and did sudo systemctl restart apache2.

最后,我启用了模块:

sudo phpenmod -v 7.3 pgsql
sudo phpenmod -v 7.3 pdo_pgsql
sudo systemctl restart apache2

然后,我创建了一个脚本,使用pdo登录到我的Postgres数据库(此处,登录凭证已替换为占位符.)

Then I created a script to use pdo to log on to my Postgres database (the logon credentials are replaced with placeholder values here.)

<?php

$params = [
    'host' => '[IP Address]',
    'user' => '[username]',
    'pwd' => '[password]',
    'db' => '[dbname]'
];

$dsn = sprintf('pgsql:host=%s;dbname=%s;user=%s;password=%s',
    $params['host'],
    $params['db'],
    $params['user'],
    $params['pwd']);

try {
    $dsn = sprintf('pgsql:host=%s;dbname=%s;unix_socket=%s',
        $params['host'], $params['db'], $params['sock']);
    $opts = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
    $pdo = new PDO($dsn, $params['user'], $params['pwd'], $opts);
} catch (PDOException $e) {
    echo $e->getMessage();
} catch (Throwable $e) {
    echo $e->getMessage();
}

?>

但是Firefox开发者控制台回显:找不到驱动程序."

But the Firefox dev console echoes back: "could not find driver."

一个线索是php 7.3在/etc/apache2/mods-available中,但不在/etc/apache2/mods-enabled中,这表明未启用它.但是,当我尝试phpenmod -v 7.3 php7.3.conf时,我得到:警告:/etc/php/7.3/mods-available下的模块php7.3.conf ini文件不存在.

One clue is that php 7.3 is in /etc/apache2/mods-available, but it's not in /etc/apache2/mods-enabled, which suggests that it's not enabled. But when I try phpenmod -v 7.3 php7.3.conf, I get: WARNING: Module php7.3.conf ini file doesn't exist under /etc/php/7.3/mods-available.

我对此做了很多研究,这些是要遵循的步骤.大部分研究是针对MySQL的,但这对PDO来说并不重要.

I've done a lot of research on this, and those are the steps to follow. Much of that research was specific to MySQL, but that shouldn't matter for PDO.

感谢您提供任何有关为什么我收到找不到驱动程序"消息的想法.

Thanks for any ideas on why I am getting the message "could not find driver."

更新:

我创建了一个脚本来运行phpinfo():

I created a script to run phpinfo():

<?php
ob_start();
phpinfo();
$info = ob_get_clean();
echo $info;
?>

但它仅返回html代码:

but it returns only html code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<style type="text/css">
body {background-color: #fff; color: #222; font-family: sans-serif;}
pre {margin: 0; font-family: monospace;}
a:link {color: #009; text-decoration: none; background-color: #fff;}
a:hover {text-decoration: underline;}
table {border-collapse: collapse; border: 0; width: 934px; box-shadow: 1px 2px 3px #ccc;}
.center {text-align: center;}
.center table {margin: 1em auto; text-align: left;}
.center th {text-align: center !important;}
td, th {border: 1px solid #666; font-size: 75%; vertical-align: baseline; padding: 4px 5px;}
h1 {font-size: 150%;}
h2 {font-size: 125%;}
.p {text-align: left;}
.e {background-color: #ccf; width: 300px; font-weight: bold;}
.h {background-color: #99c; font-weight: bold;}
.v {background-color: #ddd; max-width: 300px; overflow-x: auto; word-wrap:  break-word;}
.v i {color: #999;}
img {float: right; bo…
jquery.min.js line 2 > eval:12:21
?

但这不是我所期望的.

But that's not what I expected.

推荐答案

在我们的对话中,问题是由安装了多个版本的PHP引起的,并且Apache正在加载与预期的PHP 7.3不同的PHP(7.2)版本.

As from our conversation, the issue was caused by having multiple versions of PHP installed and Apache was loading a different version of PHP (7.2) than the expected PHP 7.3.

要解决此问题,请运行以下命令:

To resolve the issue run the following commands:

sudo a2dismod php7.2
sudo a2enmod php7.3

这将禁止Apache加载php7.2,而改为加载php7.3.

This will disable php7.2 from being loaded by Apache and load php7.3 instead.

对于您的DSN(数据源名称),您当前的配置使用的是unix_socket,它似乎不是Postgresql DSN的有效选项.此外,缺少用于unix_socket参数的$params['socket'].

For your DSN (data source name), your current configuration is using a unix_socket which does not appear to be a valid option for the postgresql DSN. Additionally the $params['socket'] to be used for the unix_socket parameter is missing.

有关更多详细信息,请参见: https://www. php.net/manual/en/ref.pdo-pgsql.connection.php

For more details see: https://www.php.net/manual/en/ref.pdo-pgsql.connection.php

如果数据库服务器正在侦听默认端口(5432),则可以使用:

If the database server is listening on the default port (5432) you can use:

try {
    $dsn = vsprintf('pgsql:host=%s;port=%s;dbname=%s;user=%s;password=%s', [
        'host' => '[IP Address]',
        'port' => '5432',
        'dbname' => '[dbname]',
        'user' => '[username]',
        'password' => '[password]',
    ]);
    $pdo = new PDO($dsn);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo $e->getMessage();
} catch (Throwable $e) {
    echo $e->getMessage();
}

DSN应该生成: https://3v4l.org/aFKAW

pgsql:host=[IP Address];port=5432;dbname=[dbname];user=[username];password=[password]

这篇关于Postgres的php pdo:“找不到驱动程序";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆