PHP PDO connection to MySQL fails, mysql_connect works fine

I try to connect to a remote MySQL database using PDO, but the error fails:

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

This is how I try 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 he fails. But:

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

works great.

Does anyone know what might be wrong with the PDO, its configuration, the parameters I set, or maybe this particular server (nazwa.pl)?

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

+6
source share
2 answers

Try replacing:

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

from

 $dsn = "mysql:host=sql.my_domain.nazwa.pl;dbname=my_db; port=3307"; 
+5
source

If you are trying to connect to the database on some other server, make sure that your Sql server gives you access to a specific port in your case 3307 from the IP address of the places where your codes are located. If both servers are the same, try with localhost or 127.0.0.1

+1
source

Source: https://habr.com/ru/post/922040/


All Articles