I'm not that awesome with PDO, been learning it to update my PHP standards, I thought I had it cracked,however I keep getting an error.
I have used this before and it works and there is nothing I can see that has changed.
echo "before connect";
$conn = new PDO('mysql:host=hostadd;dbname=dbname', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "past connect";
This is my code, the password and server address are correct, as are the variable values that are defined before this statement.
I never make it to the past connect echo though, and I always get this error:
before connectError: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
Can anyone tell me what daft mistake I am making?
This problem has nothing to do with PDO. To connect an internet service you have to supply a valid and working host name.
if you're already busy updating your coding standards, please either set the PDO error mode to return warnings or handle the Exceptions accordingly.
Apart from that, the problem is propably that "hostadd" is not a reachable host name in first place. Propably you want to use localhost or another DB server supplied by your hosting provider. If you want to store it in a value, you forgot to concatenate the string. PDO tries to reach "hostadd" which will fail just like when you go into your Terminal and type
ping hostadd