【发布时间】:2015-10-06 16:17:34
【问题描述】:
我在发送用 php 编写的后台脚本的批处理时事通讯时遇到了一些问题。
我注意到通过浏览器运行 php 和通过 php 可执行文件之间存在很多差异。我认为这是因为可执行文件直接执行的 php 脚本与 apache 没有任何交互。 例如,我必须在调用脚本之前将 DOCUMENT_ROOT 设置为环境变量或将其解析为参数,因为 $_SERVER["DOCUMENT_ROOT"] 没有定义。
现在我在尝试通过 PDO 连接到我的数据库时遇到了这个问题。以下是发生的事情:
C:/Users/hedge/Dev/PHPStorm/gpstudios/www/files/processes/send_newsletters.php hfBH6rCA 2>&1
log.js:137 Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php on line 42
Call Stack:
0.0003 126144 1. {main}() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\processes\send_newsletters.php:0
0.0016 155392 2. getConnection() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\processes\send_newsletters.php:14
0.0023 196416 3. Connection->__construct() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\inc\config.php:20
0.0023 196760 4. PDO->__construct() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php:42
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. ' in C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php on line 42
PDOException: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php on line 42
Call Stack:
0.0003 126144 1. {main}() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\processes\send_newsletters.php:0
0.0016 155392 2. getConnection() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\processes\send_newsletters.php:14
0.0023 196416 3. Connection->__construct() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\inc\config.php:20
0.0023 196760 4. PDO->__construct() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php:42
PHP Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php on line 42
PHP Stack trace:
PHP 1. {main}() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\processes\send_newsletters.php:0
PHP 2. getConnection() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\processes\send_newsletters.php:14
PHP 3. Connection->__construct() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\inc\config.php:20
PHP 4. PDO->__construct() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php:42
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. ' in C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php:42
Stack trace:
#0 C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php(42): PDO->__construct('mysql:host=loca...', '<redacted>', '<redacted>')
#1 C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\inc\config.php(20): Connection->__construct('localhost', '<redacted>', '<redacted>', '<redacted>')
#2 C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\processes\send_newsletters.php(14): getConnection()
#3 {main}
thrown in C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php on line 42
似乎无法解析 localhost,但它没有在日志中明确报告。
我该如何解决这个问题?
如果我将 PDO 构造函数中的 'localhost' 替换为 '127.0.0.1',它会起作用。显然问题在于解析本地主机。 我正在使用 proc_open() 来启动脚本。我发现如果我通过 CMD 提示符运行脚本,它就可以工作。 它看起来像这样: start /B php C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\processes\send_newsletters.php
那么问题似乎出在 proc_open...
编辑
问题仍然没有解决,但我已经找到了它的根源:
proc_open($cmd, [['pipe', 'r'],['pipe', 'w'],['pipe', 'w']], $pipes, NULL, $environment_variables);
问题在于 $environment_variables,它是一个关联数组,其中包含 - 你猜对了 - 环境变量。 我在 php.net 上遇到过这个:http://php.net/manual/en/function.proc-open.php#117912
对于那些发现使用 $cwd 和 $env 选项会导致 proc_open 失败(Windows)的人。您将需要传递所有其他服务器环境变量;
但是,即使我这样做了,它仍然无法解析 localhost。
编辑 2
问题已解决。有用的用户在 php.net 上提供的代码使用了数组($_SERVER),而实际上您只需要 $_SERVER。 所以代码现在看起来像:
proc_open($cmd, [['pipe', 'r'],['pipe', 'w'],['pipe', 'w']], $pipes, NULL, array_merge($_SERVER, $environment_variables))
嘘!谢谢大家的帮助。
【问题讨论】:
-
请出示您的代码。我怀疑您在
new PDO中传递了错误的主机名。 -
您在
E_ALL有错误报告吗?我看到你有警告但可能没有通知。您能否在Connection.php中发布相关的 pdo 构造函数,以便我们查看 DSN 字符串是如何构建的?实际上,在我看来,字符串localhost没有正确传递给 DSN,因为我认为我希望错误消息包含更多信息。 -
@Barmar Connection.php 的内容无关紧要。我只是在做
new PDO("mysql:host=$host;dbname=$db;charset=utf8", $username, $password);你明白我是通过 php 可执行文件而不是通过浏览器运行命令吗?您会注意到很多差异,无法解析 localhost 显然是其中之一。 -
解析主机名应该在 CLI 和网络服务器中工作。
host localhost说什么? -
'host' 是 Linux 命令吗?我应该在我原来的帖子中提到我正在使用 Windows XAMPP。