PDO :: __ construct (): php_network_getaddresses: getaddrinfo failed: name or service unknown

The Stack Overflow section has asked a few questions, but none of them are relevant to my case.

I use Lampp with PHP 5.4.7 and it worked fine until I developed my first PDO program in PHP.

When i use

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

to connect, I get this error. Do I need to activate something in php.ini?

+8
php pdo
source share
2 answers

"MySQL: host = 'local'; DBNAME = 'data'; encoding = UTF-8"

Your DSN format is incorrect; it must not contain these quotation marks. This is the correct format.

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

See manual

+13
source share

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

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

+3
source share

All Articles