PHP PDO connection to MySQL fails, mysql_connect works fine

Viewed 5795

I'm trying to connect to remote MySQL database using PDO, but it fails with error:

Connection failed: SQLSTATE[28000] [1045] Access denied for user 'my_user'@'some.ip.address' (using password: YES)

This is how I'm trying to connect:

$dsn = "mysql:host=sql.my_domain.nazwa.pl;dbname=my_db;port:3307";
$user = "my_user";
$password = "my_password";

try {
    $this->db = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

and it fails. But this way:

mysql_connect('sql.my_domain.nazwa.pl:3307', 'my_user', 'my_password');

works fine.

Anyone have any idea what can be wrong with PDO, its configuration, parameters I set or maybe this specific server (nazwa.pl)?

[SOLVED] Ok, so that was simple (but also tricky to notice...) syntax error, it should be a = instead of : in port part of dsn.

2 Answers
Related