php 致命错误:调用未定义的函数 pg_connect()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7438059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:40:27  来源:igfitidea点击:

Fatal error: Call to undefined function pg_connect()

phpapachepostgresql

提问by hrshd

I am trying to connect to my database (remote server) which has PostgreSQL installed in it. My PHP code is trying to connect to the database using pg_connect(), but I get the error saying:- "Fatal error: Call to undefined function pg_connect() in /var/www/website/functions.php on line 82".

我正在尝试连接到安装了 PostgreSQL 的数据库(远程服务器)。我的 PHP 代码正在尝试使用 pg_connect() 连接到数据库,但我收到错误消息:-“致命错误:在第 82 行的 /var/www/website/functions.php 中调用未定义的函数 pg_connect()”。

The line 82 simply is:

第 82 行只是:

$db = pg_connect($conn_string);
where $conn_string = "host=".$hostname." port=5432 dbname=".$dbname." user=".$db_user." password=".$db_password.""

(all variables defined earlier)

(之前定义的所有变量)

I checked many forums and the only solution suggested was locating the php.ini file which contains a line:- extension = pgsql.so (for UNIX) and extension = php_pgsql.dll (for Windows).

我检查了许多论坛,建议的唯一解决方案是找到包含一行的 php.ini 文件:- extension = pgsql.so(对于 UNIX)和 extension = php_pgsql.dll(对于 Windows)。

This statement is supposed to be commented and the solution is to uncomment it. I have tried it but still does not change the situation. The remote server has a version later than PostgreSQL v9.0.4 installed. I then installed PostgreSQL v8.4.8 on to my laptop and ran the website locally using MAMP. At first, Apache crashed for some odd reason, I fixed that problem but again I ended up with the same error as before i.e. Fatal error: Call to undefined function pg_connect()....

这个语句应该被注释,解决方法是取消注释。我已经尝试过了,但仍然没有改变这种情况。远程服务器安装了 PostgreSQL v9.0.4 之后的版本。然后我将 PostgreSQL v8.4.8 安装到我的笔记本电脑上,并使用 MAMP 在本地运行该网站。起初,Apache 由于某种奇怪的原因崩溃了,我解决了这个问题,但我再次遇到了与之前相同的错误,即致命错误:调用未定义的函数 pg_connect()....

I also ran the phpinfo()and it showed that the php version does support the PostgreSQL module.I have spent an entire day searching for the solution but have been unsuccessful. This is my first project developing a website and I am out of wits. Any kinda help will be highly appreciated.

我也跑了phpinfo(),结果显示php版本确实支持PostgreSQL模块。我花了一整天的时间寻找解决方案,但没有成功。这是我第一个开发网站的项目,我已经失去理智了。任何帮助将不胜感激。

phpinfo() gives me a huge list of things at the terminal but the listings relevant to PostgreSQL are as follows:-

phpinfo() 在终端为我提供了大量内容,但与 PostgreSQL 相关的列表如下:-

pdo_pgsql

PDO Driver for PostgreSQL => enabled
PostgreSQL(libpq) Version => 9.0.4
Module version => 1.0.2
Revision =>  $Id: pdo_pgsql.c 306939 2011-01-01 02:19:59Z felipe $ 

pgsql

PostgreSQL Support => enabled
PostgreSQL(libpq) Version => 9.0.4
Multibyte character support => enabled
SSL support => enabled
Active Persistent Links => 0
Active Links => 0

Directive => Local Value => Master Value
pgsql.allow_persistent => On => On
pgsql.auto_reset_persistent => Off => Off
pgsql.ignore_notice => Off => Off
pgsql.log_notice => Off => Off
pgsql.max_links => Unlimited => Unlimited
pgsql.max_persistent => Unlimited => Unlimited

I had restarted MAMP after every edit I made since it was mentioned in every post I have read so far. I believe that resets both Apache and php.

每次编辑后我都重新启动了 MAMP,因为到目前为止我读过的每篇文章都提到了它。我相信这会重置 Apache 和 php。

'pqsql.so' (which is the UNIX equivalent of 'php_pqsql.dll' in Windows) is present in the 'extension' directory. I also copy-pasted the 'pqsql.so' file on to the Apache/bin directory but it did not give me any change.

“pqsql.so”(在 UNIX 中相当于 Windows 中的“php_pqsql.dll”)存在于“extension”目录中。我还将“pqsql.so”文件复制粘贴到 Apache/bin 目录,但它没有给我任何更改。

I am not running php in the command line primarily. I just was curious to see what phpinfo() would give me relevant to pgsql which I have mentioned in my reply above.

我主要不是在命令行中运行 php。我只是想知道 phpinfo() 会给我什么与我在上面的回复中提到的 pgsql 相关的内容。

I am still working on the tools you have mentioned and will respond as soon as I get any results.

我仍在研究您提到的工具,并会在得到任何结果后立即回复。

Thanks, H

谢谢,H

回答by Scott Marlowe

You need to install the php-pgsql package or whatever it's called for your platform. Which I don't think you said by the way.

您需要安装 php-pgsql 包或任何为您的平台调用的包。顺便说一下,我认为你不是这么说的。

On Ubuntu and Debian:

在 Ubuntu 和 Debian 上:

sudo apt-get install php5-pgsql

回答by Angelin Nadar

Easy install for ubuntu:

轻松安装 ubuntu:

Just run:

赶紧跑:

sudo apt-get install php5-pgsql

then

然后

sudo service apache2 restart //restart apache

or

或者

Uncomment the following in php.ini by removing the ;

通过删除 php.ini 中的以下内容取消注释 ;

;extension=php_pgsql.dll

then restart apache

然后重启apache

回答by davidleosam

Fatal error: Call to undefined function pg_connect()...

致命错误:调用未定义的函数 pg_connect()...

I had this error when I was installing Lampp or xampp in Archlinux,

我在 Archlinux 中安装 Lampp 或 xampp 时遇到了这个错误,

The solution was edit the php.ini, it is located in /opt/lampp/etc/php.ini

解决办法是编辑php.ini,它位于/opt/lampp/etc/php.ini

then find this line and uncomment

然后找到这一行并取消注释

extension="pgsql.so"

then restart the server apache with xampp and test...

然后用xampp重新启动服务器apache并测试...

回答by JRL

Edit. I just noticed you were mentionning MAMP. My advice is for Windows but may be useful if you know what corresponding tools to use.

编辑。我刚刚注意到你提到了 MAMP。我的建议适用于 Windows,但如果您知道要使用哪些相应的工具,可能会很有用。

Things to try:

尝试的事情

  • Have you restarted PHP and Apache since your editing of php.ini?

  • Is the php_pgsql.dll found in your php\ext directory?

  • Are you running php as a module? If so, try copying the php_pgsql.dll file in the Apache\bin directory.

  • Are you running PHP from the command line with a flag specifying a different php.ini file?

  • You could try using a tool such as Sysinternals' Filemon to view what files are attempting to be accessed when running PHP.

  • You could try using a tool such as Dependency Walker to look at the dependencies for the postgreSQL DLL, in case you have a missing dependency. Quick search brought up lddfor Unix.

  • 您在编辑 php.ini 后是否重新启动了 PHP 和 Apache?

  • php_pgsql.dll 是否在您的 php\ext 目录中?

  • 您是否将 php 作为模块运行?如果是这样,请尝试复制 Apache\bin 目录中的 php_pgsql.dll 文件。

  • 您是否使用指定不同 php.ini 文件的标志从命令行运行 PHP?

  • 您可以尝试使用诸如 Sysinternals 的 Filemon 之类的工具来查看运行 PHP 时尝试访问的文件。

  • 您可以尝试使用 Dependency Walker 等工具查看 postgreSQL DLL 的依赖项,以防您缺少依赖项。快速搜索为 Unix带来了ldd

回答by Chetan

  1. Add 'PHPIniDir "C:/php"' into the httpd.conf file.(provided you have your PHP saved in C:, or else give the location where PHP is saved.)
  2. Uncomment following 'extension=php_pgsql.dll' in php.ini file
  3. Uncomment ';extension_dir = "ext"' in php.ini directory
  1. 将'PHPIniDir "C:/php"' 添加到 httpd.conf 文件中。(前提是你的 PHP 保存在 C: 中,否则给出保存 PHP 的位置。)
  2. 取消注释 php.ini 文件中的“extension=php_pgsql.dll”
  3. 取消注释 ';extension_dir = "ext"' 在 php.ini 目录中

回答by Csongor Halmai

I had the same symptom in win7. I got this script:

我在win7下也有同样的症状。我得到了这个脚本:

<?php
    phpinfo();
    pg_connect("blah");

When I executed the phpinfo.php script via apache (http://localhost/phpinfo.php) then I got the error message: Call to undefined function pg_connect() in...

当我通过 apache ( http://localhost/phpinfo.php)执行 phpinfo.php 脚本时,我收到错误消息: Call to undefined function pg_connect() in...

When I executed the samescript from command line (php phpinfo.php) then I got the expected message: PHP Warning: pg_connect(): Unable to connect to PostgreSQL server: missing "=" after "blah"

当我从命令行 (php phpinfo.php)执行相同的脚本时,我得到了预期的消息: PHP 警告:pg_connect():无法连接到 PostgreSQL 服务器:在“blah”之后缺少“=”

In both cases the expected php.ini was used:

在这两种情况下,都使用了预期的 php.ini:

Loaded Configuration File   C:\Program Files (x86)\php\php.ini 

but the pgsql section was completely missing from the phpinfo in case of the apache-based execution and it was present in the command-line-based execution.

但是在基于 apache 的执行的情况下,phpinfo 中完全缺少 pgsql 部分,并且它存在于基于命令行的执行中。

The solution was that I added the following line to the apache httpd.conf:

解决方案是我在 apache httpd.conf 中添加了以下行:

LoadFile "C:/Program Files (x86)/php/libpq.dll"

It seems that for some reason this file is not loaded automatically when apache runs the php script but it is loaded if I run the php script from the command line.

似乎出于某种原因,当 apache 运行 php 脚本时,该文件不会自动加载,但如果我从命令行运行 php 脚本,则会加载它。

I hope it helps.

我希望它有帮助。

回答by Alf Newman

For php 5.4 on Centos 6.10, we include these lines in php.ini

对于 Centos 6.10 上的 php 5.4,我们在 php.ini 中包含这些行

extension=/opt/remi/php54/root/usr/lib64/php/modules/pdo.so
extension=/opt/remi/php54/root/usr/lib64/php/modules/pgsql.so
extension=/opt/remi/php54/root/usr/lib64/php/modules/pdo_pgsql.so

It works.

有用。

回答by user9548

I also had this problem on OSX. The solution was uncommenting the extension = pgsql.soin php.ini.defaultand deleting the .defaultsuffix, since the file php.iniwas not there.

我在 OSX 上也有这个问题。解决方案是取消注释extension = pgsql.soinphp.ini.default并删除.default后缀,因为该文件php.ini不存在。

If you are using XAMPP, the php.ini file resides in /XAMPP/xampfiles/etc

如果您使用 XAMPP,则 php.ini 文件位于 /XAMPP/xampfiles/etc

回答by SPRBRN

For those of you who have this problem with PHP 5.6, you can use the following command:

对于那些在 PHP 5.6 中遇到此问题的人,可以使用以下命令:

yum install php56w-pgsql

For a list of more package names for PHP 5.6, open the following link and scroll down to packages:

有关 PHP 5.6 的更多包名称列表,请打开以下链接并向下滚动到包:

PHP 5.6 on CentOS/RHEL 7.0 and 6.6 via Yum

CentOS/RHEL 7.0 和 6.6 上的 PHP 5.6 通过 Yum

回答by quetzaluz

I encountered this error and it ended up being related to how PHP's extension_dir was loading.

我遇到了这个错误,它最终与 PHP 的 extension_dir 加载方式有关。

If upon printing out phpinfo() you find that under the PDO header PDO driversis set to no value, you may want to check that you are successfully loading your extension directory as detailed in this post:

如果在打印 phpinfo() 时发现 PDO 标头下的 PDO驱动程序设置为no value,则可能需要检查是否成功加载了扩展目录,如本文所述:

https://stackoverflow.com/a/14786808/2578505

https://stackoverflow.com/a/14786808/2578505