Without your PDO we can not know what you did wrong, since the error is in it, but anyway you can say that this SQLSTATE[HY000] [2002]
message indicates that you configured something wrong in this:
'mysql:dbname=testdb;host=mysql.host_de_exemplo.com'
For example:
$dsn = 'mysql:dbname=testdb;host=mysql.host_de_exemplo.com';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
- Or you did not properly set
$dsn
, leaving it empty
- Or
host=
you pointed does not exist
- Or
host=
is empty
-
Or you put a wrong port, for example:
$dsn = 'mysql:dbname=testdb;host=mysql.host_de_exemplo.com:8000';
Usually the default port is 3306
-
Or you're running this script on an online server and not on your machine, but you're still trying to access the localhost
domain, like this:
$dsn = 'mysql:dbname=testdb;host=localhost';
Only in online servers rarely mysql server is at the same address of the http server, ie it does not have access via localhost, it is necessary to point the correct address that the host tells you, usually it is only the IP code, usually is informed in your panel (like cpanel or something similar), example:
$dsn = 'mysql:dbname=testdb;host=169.3.23.1';