Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

pdo

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

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

When I use

$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?

like image 468
Naveen Avatar asked Jan 27 '14 09:01

Naveen


3 Answers

"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

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

See Manual

like image 198
Hanky Panky Avatar answered Nov 18 '22 19:11

Hanky Panky


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

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

like image 44
Cjmarkham Avatar answered Nov 18 '22 20:11

Cjmarkham


For the case when you have a .env file.

DO NOT USE ' (single quote) for values on DB settings.
For example the following must be written:

DB_CONNECTION=mysql
DB_HOST=mariadb
DB_PORT=3306

NOT!!! DB_CONNECTION='mysql' etc.

like image 1
LucianDex Avatar answered Nov 18 '22 19:11

LucianDex