php PDO::__construct(): php_network_getaddresses: getaddrinfo 失败: 名称或服务未知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21377348/
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-25 04:05:43  来源:igfitidea点击:

PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known

phppdo

提问by Insane Coder

There are tons of questions asked on this topic on Stack Overflow, but none of them matches my case.

在 Stack Overflow 上有很多关于这个主题的问题,但没有一个符合我的情况。

I am using Lampp with PHP 5.4.7 and it was running fine until I developed my first PDO program in PHP.

我在 PHP 5.4.7 中使用 Lampp,它运行良好,直到我用 PHP 开发了我的第一个 PDO 程序。

When I use

当我使用

$con=new PDO("mysql:host='localhost';dbname='data';charset=utf8",'root','');

$con=new PDO("mysql:host='localhost';dbname='data';charset=utf8",'root','');

for connection I get this error.Do I need to activate something in php.ini?

对于连接,我收到此错误。是否需要在 php.ini 中激活某些内容?

回答by Hanky Panky

"mysql:host='localhost';dbname='data';charset=utf8"

"mysql:host='localhost';dbname='data';charset=utf8"

Your DSN format is wrong, it shouldn't have those quotes in there. This is the right format

您的 DSN 格式错误,其中不应该包含这些引号。这是正确的格式

 //$con=new PDO($dsn, $user, $password);
 $con=new PDO('mysql:dbname=testdb;host=127.0.0.1','root',''); 

See Manual

参见手册

回答by Carl Markham

Try using localhost ip instead: 127.0.0.1and remove the quotes around dbname and host:

尝试使用 localhost ip 代替:127.0.0.1并删除 dbname 和 host 周围的引号:

$con=new PDO("mysql:host=127.0.0.1;dbname=data;charset=utf8",'root','');

$con=new PDO("mysql:host=127.0.0.1;dbname=data;charset=utf8",'root','');