duankuang1046 2012-08-03 11:51
浏览 134
已采纳

PHP PDO与MySQL连接失败,mysql_connect工作正常

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条回答 默认 最新

  • du3979 2012-08-03 11:57
    关注

    Try replacing:

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

    with

    $dsn = "mysql:host=sql.my_domain.nazwa.pl;dbname=my_db; port=3307";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?